Summary of basic knowledge points of Java entry (detailed)

1.1 Illustration

1.1.1 Java basic knowledge points

insert image description here

1.1.2 Related content of Java basic syntax

insert image description here

1.2 Keywords

Definition: A string (word) given a special meaning by the Java language and used for special purposes.
Features: All letters in the keyword are lowercase
insert image description hereinsert image description here

1.3 Identifiers

1.3.1 The concept of identifiers

The character sequence that Java uses to name various variables, methods, and classes is called an identifier. Anywhere
you can name yourself is called an identifier.

1.3.2 Legal rules for defining identifiers

Consists of 26 uppercase and lowercase English letters, 0-9, _ or $.
Numbers cannot start.
Keywords and reserved words cannot be used, but keywords and reserved words can be included.
Java is strictly case-sensitive, and the length is unlimited.

1.3.3 Some naming conventions in Java

Package name: All letters are lowercase when composed of multiple words.
Class name, interface name: When composed of multiple words, the first letter of all words is capitalized.
Variable name, method name: When composed of multiple words, the first letter of the first word is lowercase, and the first letter of each word is uppercase at the beginning of the second word:
Constant name: All letters are uppercase. When there are multiple words, each word is connected with an underscore: PRIVATE_CONSTANT For
example, public static final double PI=3.14; //Use the final keyword to modify a constant.

1.4 Variables

Definition format: data type variable name
Points to note:
the scope of the variable: a pair of { } is valid.
Values ​​need to be initialized before use.

1.4.1 Classification of variables

① According to the data type
insert image description here
② According to the declaration position Outside
the method, the variables declared in the class body are called member variables (global variables).
Outside the method body, variables declared inside the class body are called local variables.

Note: the similarities and differences between the two in terms of initialization values: the
same point: both have a life cycle
Difference: local variables (including those in the main function) need to be explicitly initialized (assigned initial values) except for formal parameters .

1.4.2 Integer Types

Byte, short, int, long
Java integer types have a fixed table number range and field length (uniform on any platform).
Integer constants in java are of int type by default, and 'l' or 'L' must be added after the declaration of long type constants.
insert image description here
Memory rules, from small to large, occupy 1 2 4 8 bytes of storage space respectively.

1.4.3 Floating point types

Java's floating-point constants are double-type by default. To declare a float-type constant, add 'f' or 'F' afterward.
There are two representations of floating-point constants:
Decimal number form: such as: 5.12 512.0f .512 (must have a decimal point)
Scientific notation form: such as: 5.12e2 512E2 100E-2
insert image description here

1.4.4 Character type char

char (2 bytes)
The char type can be operated on. Because it corresponds to the Unicode code.
It will be automatically converted to int type when used for calculation.
Java also allows the use of the escape character '\' to convert the following character into a special character constant. For example: char c3 = '\n'; // '\n' means newline

1.4.5 Boolean type boolean

The boolean type data only allows the values ​​true and false, no null. The default initial value is false , and it is
not possible to replace false and true with 0 or non-zero integers, which is different from the C language.

1.5 AScii code and Unicode code

Ascii code is a code developed by people in order to represent characters in the computer when the computer was invented. Ascii is represented by 7 bits in a byte, the range is 0x00-0x7F, a total of 128 characters
Extended ASCII, and later extended based on the ascii table, a total of 256 characters.
Later, in order to uniformly encode all the text symbols of people all over the world, the UNICODE standard character set was developed. Unicode is just a symbol set, it only specifies the binary code of the symbol, UNICODE uses two bytes to represent a character. The range of UNICODE is 0x0000-0xFFFF, a total of more than 60,000 characters, of which more than 40,000 characters are occupied by Chinese characters.
UTF-8 is the most widely used implementation of Unicode on the Internet.

1.6 Basic data type conversion

Automatic type conversion: Types with small capacity are automatically converted to data types with large capacity. The data types are sorted by capacity as follows:
insert image description here
① When there are multiple types of data mixed operation, the system first automatically converts all the data into the data type with the largest capacity, and then performs the calculation.
②byte, short, and char will not be converted to each other, and the three of them are first converted to int type during calculation.
③ When the value of any basic type is connected with the string value (+), the value of the basic type will be automatically converted to the string type.

Both byte and short will be converted when it comes to assignment, but special shipping will not

String class

The value null can be assigned to any reference type (class, interface, array) variable to indicate that the address stored in the reference type variable is empty.
The String class is a reference type and can be assigned null.
A String object cannot be changed after it is created, and a new object needs to be created every time it is copied.

coercion

① The inverse process of automatic type conversion converts a data type with a large capacity to a data type with a small capacity. When using it, you need to add a coercive conversion character, such as (int), but it may cause precision reduction or overflow, and special attention should be paid to it.
②In general, strings cannot be directly converted to basic types, but through the wrapper classes corresponding to basic types, strings can be converted to basic types.
Such as: String a = "43"; int i = Integer.parseInt(a);
boolean type cannot be converted to other basic data types.

There is no mutual conversion between char, byte, and short. It involves the conversion of operations to int, and conversion to int will not automatically convert back. As follows:
insert image description here
insert image description here
insert image description here
insert image description here
But this is no problem, as follows

insert image description here
insert image description here

1.7 base

Binary: start with 0b or 0B
Decimal: normal, no need to add anything at the beginning.
Octal: start with the number 0, start
with the number 0, start with 0x or 0X, 0-9 and AF, AF is not case-sensitive.

1.7.1 Original code, complement code

All numbers exist in binary form at the bottom of the computer.
All numbers exist in binary form at the bottom of the computer.
The original code and complement code of positive numbers are the same.
The complement of a negative number is: except the sign bit remains unchanged, the rest of the positions are inverted +1.

1.7.2 Conversion between bases

insert image description here

1.8 Operators

An operator
is a special symbol used to represent data operations, assignments, and comparisons. There are eight types in total. (I only pick the points to pay attention to here)
①Bit
operators ②Arithmetic
operators
③Assignment operators④Comparison operators (relational operators)
⑤Logical
operators⑥Ternary operators

1.8.1 Bitwise operators

insert image description here
insert image description here

1.8.2 Arithmetic operators

insert image description here
①+ is from left to right, such as the following:
"+" can convert non-strings into strings in addition to the function of adding strings. For example:

System.out.println("5+5="+5+5); //打印结果是?

5+5=55

System.out.println(5+5+"=10"); //打印结果是?

10=10

②i++ is used first and then added, ++i is used first and then used
③i++ is more efficient than i=i+1 ④Example

(very important):

public class Test3{
    
    
	public static void main(String[] args){
    
    
		int i = 0;		
			i = i++;
			System.out.println("i =" + i);
	}
}

Result: i=0

Analysis:
i = i + 1;
open up space in the memory, record the initial value of i 0, open up another temporary variable cache space, record the temporary value of the i variable, and then perform the self-increment operation of i i = i+ 1. At this time, i = 1, covering 0 in the original space. At this time, the operation on the right side is completed, and then the assignment operation is performed, that is, the value of temp is assigned to i, and the side length of i is 0.

is equivalent to:

int temp = i; //先使用i的值
i = i + 1;
i = temp;

⑤ About i += 1 and i = i + 1 in Java

public class test {
    
    
	public static void main(String[] args) {
    
    
		short s = 1;
		s = s + 1;   //编译错误
		s += 1;      //编译通过
	}}
编译

The error is due to the fact that s1+1 will automatically promote the type of the expression to int during the operation,
and then a cast error will occur when it is assigned to s1 of the short type.
Although the two expressions x+=y and x=x+y are in general The case works in general, but there are some subtle differences when running in a Java environment. The difference is that, in addition to implementing the + function, += also automatically performs type conversion based on the type of the received variable.

Question: If x is a variable of type short, will x++ and ++x perform casts: no.

1.8.3 Assignment Operators

Distinguish the difference between == and =.
Boolean types cannot be converted to other types

1.8.4 Comparison Operators

insert image description here

1.8.5 Logical Operators

insert image description here
①The difference between "&" and "&&":
when a single &, the left side is true or false, the right side is operated;
when a double & is used, if the left side is true, the right side participates in the operation, and if the left side is false, then the right side does not participate in the operation.
The difference between "|" and "||" is the same, || means: when the left side is true, the right side does not participate in the operation.

②Short-circuit and && need to pay attention to a problem.
If the right side of && is an operation with a variable value such as i++, it is better to use &, otherwise the condition on the left side of && may be judged to be false, and i++ will not be executed.
③^ is XOR, the same is 0, and the difference is 1.

1.8.6 Ternary Operators

insert image description here

1.8.7 Operator precedence

insert image description here

Unary operators (++, –)

1.9 Program Flow Control Statements

1. Sequence structure: The program executes directly from top to bottom, without any judgment or jump in the middle
. 2. Branch structure: According to judgment, a certain piece of code is selectively executed. There are two branch statements, if...else and switch
. 3. Loop structure: Execute a certain piece of code repeatedly according to the loop condition. There are three loop statements: while, do...while, and for. JDK1.5 provides foreach loop, which is convenient to traverse collection and array elements.

The four components of the loop statement:
initialization part (init_statement)
loop condition part (test_exp)
loop body part (body_statement)
iteration part (alter_statement)
if statement has three formats:

1.  if (条件表达式) {
    
    
	执行代码块;
     }


2.  if (条件表达式) {
    
    
	执行代码块;
      } else {
    
    
	执行代码块;
      }

3.  if (条件表达式1) {
    
    
	执行代码块1} else if (条件表达式2) {
    
    
	执行代码块2} else if (条件表达式3) {
    
    
	执行代码块3}
       …… else {
    
    
	执行代码块n;
       }

Switch usage:

switch(变量){
    
    
case 常量1:
	语句1;
	break;
case 常量2:
	语句2;
	break;
… …
case 常量N:
	语句N;
	break;
default:
	语句;
	break;
 } 

The return value of the expression in switch(expression) must be one of the following types: byte, short, char, int, String, enumeration;
the value in the case clause must be a constant, and in all case clauses The values ​​of should be different; the
default clause is optional, and default is executed when there is no matching case.
The break statement is used to make the program jump out of the switch statement block after executing a case branch; if there is no break, the program will execute sequentially to the first break statement or directly to the end of the switch (this phenomenon is called penetration).

return: is not specifically used to end a loop, its function is to end a method. When a method reaches a return statement, the method will be terminated.

Comparison of switch and if statements:
If the specific value of the judgment is not many, and it conforms to the four types of byte, short, int, and char. Although both statements can be used, the switch statement is recommended. Because the efficiency is slightly higher.

Other cases: For interval judgment, for boolean type judgment, use if, if has a wider range of use.

1.10 Methods

Format:
Modifier return value type method name (parameter type 1 formal parameter 1, parameter type 2 formal parameter 2, ....) {
program code;
return return value;
}
Form parameter: used to receive external input when the method is called data variables.
Parameter type: It is the data type of the formal parameter.
Return value: The data returned by the method to the calling program after execution.
Return Type: The data type of the result to be returned by the method.
Arguments: The data that is actually passed to the formal parameters of the function when the method is called.

1.10.1 Method overloading

insert image description here
Method overloading:
is the argument in the same class. The prerequisite is the same class, no overloading between different classes.
In the same class, methods with the same name and different parameter lists are called method overloading. No matter what else, just look at the formal parameter list, and have nothing to do with the return value type.
For example the following picture:
insert image description here
insert image description here

Method signature: method name + return value type + form a list

1.10.2 Method parameter passing

How to pass the actual parameter value to the method in Java?
There is only one way to pass parameters to a method in Java: pass by value. That is, a copy (copy) of the actual parameter value is passed into the method, while the parameter itself is not affected.

insert image description here
Looking at a topic, the reason why the output value of str does not change is that what is passed into the function is only a copy of str.
However, the reason for the change of the ch value defined by char[ ] is that although the passed function is a copy of ch, the copy variable stores it. It is also an address. When operating ch[0], that is, when operating a specific array storage value, it is located through the address, and directly locates the entity pointed to by the address stored in the ch pointer, and directly changes the real value.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324156670&siteId=291194637