Java review, java knowledge points and Java interview questions (1)


J2SE(Java 2 Platform Standard Edition)

J2ME(Java 2 Platform Micro Edtion)

J2EE(Java 2 Platform Enterprise Edition)


Java language features: open source cross platform (JVM), object-oriented, etc



JRE (JVM and jjava Class Library)

JDK contains JDK, as well as other development tools


Java source code — > javac compilation — > Java bytecode file — > Java execution — > running result


The executable file is recorded in the path environment variable, such as exe. To find the executable file in the current path first, if it is not found, go to the path configured in the path environment variable

The classpath environment variable records the directory where the Java class runs


Java language foundation

Keywords: all lowercase are displayed in different colors in IDE, goto and const exist as reserved words

Identifier: name variables, classes, interfaces, methods, etcAnd nomenclature

Class or interface: uppercase

Method or variable: lowercase

Constant: all upper case, use “_” between words separate

Note: / / / * * / * **/

Constant: literal constant: 1. String constant 2. Integer constant 3. Decimal constant 4. Character constant 5. Boolean constant 6. Null constant

Binary correlation:

  1byte = 8bit;1k = 1024byte; 1m = 1024k; 1g = 1024m;1t = 1024g

Binary (starting with 0b), octal (starting with 0), decimal, hexadecimal (starting with 0x) conversion

Four bits of 8421BCD code represent a decimal code, decimal 157 = 0001 0101 0111

Original code, inverse code and complement code

Source code: the most significant sign bit 0 is positive and 1 is negative, and the rest are numeric bits

Inverse code: the positive inverse code is the same as the original code, the negative inverse code is the same as the sign bit, and the numerical bit is reversed

Complement: the positive complement code is the same as the original code, and the negative complement code is the last digit plus one of the inverse code

Variable: data type variable name = initialization value

Data type:

Eight basic data types:

Byte: eight bit, one byte, signed integer represented by binary complement, minimum: – 128 (- 2 ^ 7) maximum: 127 (2 ^ 7-1)

Short: 16 bit, two byte, signed integer represented by binary complement, minimum: – 32768 (- 2 ^ 15) maximum: 32767 (2 ^ 15-1)

Int: 32-bit, four byte, signed integer with binary complement, minimum: – 2 ^ 31, maximum: 2 ^ 31-1

Long: 64 bit, eight byte, signed integer represented by binary complement, minimum: – 2 ^ 63 maximum: 2 ^ 63-1 default: 0l

Float: single precision 32-bit four byte floating-point number, default value: 0.0F

Double: double: Double 64 bit 8-byte floating-point number default value 0.0d default type of floating-point number bit double type

Boolean: true / false one byte

Char: a single 16 bit Unicode character, occupying two bytes, with a minimum value of\U0000 (i.e. 0); the maximum value is\UFFFF (65535), char data type can store any character; (can store Chinese characters)

Integer default int type, floating-point number default double type

The level from low to high is: byte, char, short – > int – > float – > long – > double

Automatic type conversion (default conversion): from low-level to high-level, the system will automatically convert;

Cast: when to use? Assign a high-level number to a variable lower than the number; target data type variable = (target data type) (converted data)

Although float takes 4 bytes and long takes 8 bytes, float indicates that the range is larger than long (reason: omitted, you can Google by yourself)

//Interview question: will the following statements be wrong
byte a = 1 , b;fo
B = a + A; // an error is reported and cannot be compiled because a is converted to int by default, and then cast from int to byte
//Interview questions, what are the following outputs:
byte a = (byte) 214;
//Because cast, only the last byte is truncated, and this byte is the complement. All we have to do is to convert the complement to the original code
//Output 98, because 'a' is of char type, it is converted to int 97 according to acsii table
System.out.println('a'+1);   // 98
System.out.println("a"+1);  // a1
System.out.println('a'+1+"a"); //98a
//The + after the string is the string splicing operation


The arithmetic operator: / “is a division. Division of integers can only get integers. If you want a decimal, you need to add a decimal point. 0

“%” is the surplus

++And –, the effect is different before and after the variable, the first is the self increase or decrease, and then participate in the operation, and the last is the parameter operation, and then the self increase or self decrease

Note: precedence of arithmetic operators

Assignment operator: = + = – = * = / =%=

short f = 1;
f = f+1;
//Cannot convert from int to short will be reported because it will be converted to int type by default
short f = 1;
f +=1;
//No error is reported because the extension operator contains cast, equivalent to f = (data type of F) (F + 1)

Relational operator: = =! = > = < br<=

Logical operators: & (and) | (or) ^ (XOR)! (non) & (short circuit and) | (short circuit or)

&Whatever the result on the left, the right participates in the operation. If the left side is false, then there is no parameter and operation on the right side.

|: both sides of the operation. ||: short circuit or, if the left side is true, then the right side is not involved in the operation.

Bit operator: & (and operation) | (or operation) ^ (exclusive or operation) ~ (bitwise reverse) < > (right shift) > > (unsigned right shift)

int a =1,b =2;
//Java implements the exchange of two integer variables
//Method 1: using an intermediate variable advantages: the simplest and best understood way
//Method 2: you can use two numbers to sum and then subtract. Disadvantage: if the values of X and y are too large, the precision will be lost if the value of int exceeds.
a = a + b;
b = a - b;
a = a - b;
//Method 3: exchange data by means of bit operation. The principle is: if a number is different or the same number twice, the result will still be that number, and it will not exceed the range of int (the best implementation)
a = a^b;
b = a^b;
a = a^b;
//Method 4: using reflection is the most troublesome and low cutting efficiency. It's totally amazing
//The most effective way to figure out what is 2 times 8?
//2 < < 3; it means that 2 is shifted to the left by 3 bits: 2 * 2 ^ 3 (the third power of 2) = 2 * 8;
//Because if a number is shifted n bits to the left, it is equivalent to the nth power of 2. If a number is multiplied by 8, it can be shifted to the left by 3 bits, and the efficiency of the operator is the highest. Therefore, the most effective method for 2 times 8 to be several is 2 < < 3.

Ternary operator: comparison expression? Expression 1: expression 2;

If statement: if (judgment statement) {execution statement} else if (judgment statement) {execution statement} else {execution statement}

Switch statement: data types supported by switch expression: basic data type: byte, short, char, int; wrapper data type: byte, short, character, integer; enumeration type: enum; string type: string (supported by JDK 7 +)

Note: the case must be followed by a break, otherwise the program will continue to execute case by case until the case or default of the last break appears.

The value after case can only be constant, not variable, and the value after multiple cases cannot be repeated

For loop statement: for (initialization statement; judgment statement; control statement) {execution statement;}

While loop statement: initialization statement while (judgment statement) {execution statement; control statement;}

For can be used as much as possible, because the variables in the initialization statement in for can disappear in memory as soon as possible, and the variables in the control statement can be used continuously by using the while loop

Do… While loop statement: initialization statement do {execution statement; control statement;} while (judgment statement); do… While loop executes loop body at least once

Control jump statements: break to jump out of a single loop; to jump out of a specified loop and use a tag name (not commonly used)

Continue jumps out of one loop and into the next

Method: Format: modifier returns value type method name (parameter type, parameter name,…) {execute statement; return return value;}

Actual parameter: the parameter actually participating in the operation; formal parameter: method defined, used to receive the actual parameter

Method overload: the method name is the same, the number of parameters and parameter types are different; it is independent of the return value type, only the method name and parameter list are looked at; when calling, the virtual machine distinguishes the same method name through the different parameter list

Array: stores basic data types and reference data types.

Format: data type array name;

Array type [] [] array name = new data type [M] [n]; data type [] variable name = new data type [M] []; data type [] variable name = {1,2,3}, {1}, {1,2}};

Initialization: dynamic initialization: specify the array length, and the initial value is allocated by the system: data type [] array name = new data type] [array length]

Static initialization: Specifies the initial value of each element, and the system judges the array length: array type [] array name = {1, 2, 3}

Traversal array:

Get maximum value:

Array reverse order:

Find element:

Parameter transfer in Java

Basic type: the change of formal parameters has no effect on actual parameters.

Reference type: the change of formal parameters directly affects the actual parameters. Because the reference type is a formal parameter, the address value is called to modify the same block of memory.


Memory allocation in Java:

Stack: stores local variables; releases when used; two references of stack memory point to the same heap memory space, regardless of which stack memory, the operations of variables in memory are directed at the same place in the heap

Heap: new things; each new thing has an address value; each variable has a default value of byte, short, int, long 0; float, double 0.0; char ‘; u0000’; Boolean false; reference type null; it becomes garbage after use, and is not recycled immediately, waiting for GC to recycle when it is idle

Method area:

Local method area: