12 cold Java knowledge you will encounter in the interview, how much do you know?

Usually, in the interview, the interviewer will raise some "cold" basic knowledge, such as the number of bytes occupied by basic data types, or the difference between Unicode and UTF-8. At this time, many applicants The person will get the wrong answer. In the usual encoding process, division calculations are often used. For example, if all elements in a list with a length of 1 million are divided by 16, the operation of the CPU on element<<4 will be faster than element /16 operation...

Therefore, this article summarizes some basic knowledge that is easy to be forgotten, hoping to help programmers easily deal with the basic technical problems of interviewers and improve coding efficiency. This article is suitable for technical personnel with a certain Java foundation, and some of the content may be more difficult for beginners.

1. The basic data types and memory size in Java

(1) Shaping
byte 1 byte
short 2 bytes
int 4 bytes
long 8 bytes

(2) Floating point type
float 4 bytes
double 8 bytes

(3) Character type
char 2 bytes (Unicode-16)

(4) Boolean type The
boolean type is special. Although the Java virtual machine defines the boolean type, the support of the boolean type by the virtual machine is limited, and there is no separate JVM instruction for the boolean value. After the expression that manipulates the boolean value is compiled, it uses the int data type of the JVM, which occupies 4 bytes.
The JVM also does not directly support boolean arrays. After the boolean array is compiled, its elements use byte data type, with 0 for false and 1 for true, that is, the elements of the boolean array only occupy one byte.

If you need more interview materials from major companies, you can also click to enter directly and get it for free! Password: CSDN

2. The relationship between UTF-8 and Unicode

Unicode is a unified encoding standard that uniquely encodes all existing characters. In the first Unicode version, two bytes (16 bits) were used to represent a character. Note that the byte here does not refer to a storage unit in computer memory, but a mathematical length unit. However, the length occupied by a Unicode character stored in memory requires a specific encoding rule, such as UTF-8. Therefore, Unicode is only an encoding standard, and UTF-8 is an implementation of this standard. UTF-8 specifies the space occupied by a Unicode character in memory (the space occupied by English and Chinese is different, and interested readers can See related information).

Code points refer to those numbers that can be used to encode character sets. For example, in the 16-bit Unicode character set, the encoding of the character "A" is U+0041, then 0041 is a code point.

The code unit refers to the unit of space occupied by the character. For example, in UTF-32, one code unit is 32 bits, and one character occupies 32 bits, which happens to use one code unit, which consumes a lot of memory. In UTF-16, a code unit is 16 bits, the value U+0000 to U+FFFF encoding corresponds to a character, each character occupies a code unit, but for those supplementary characters beyond this range, two codes are required Such units (ie 32 bits). In UTF-8, a code unit is 8 bits. UTF-8 uses a sequence of one to four bytes to encode Unicode code points. The principle is the same as UTF-32 and UTF-16.
Insert picture description here

3.String string constant

In the Java language, a String string constant corresponds to a String object, and it cannot be changed and inherited (because the String class is modified by the final keyword). Java language is designed in this way, mainly to make string constants (note that string constants, string variables do not conform to the rules mentioned here) can be shared, because JVM puts string constants in a public storage pool, and different variables The same string constant can be quoted.
Insert picture description here
The result of running the above code is: true. This means that a and b refer to the same String object.

4. Basic data type conversion rules

In a two-operand operation, a lower-level type is converted to a higher-level type according to the operand type.
1) As long as one of the two operands is of type double, the other will be converted to type double, and the result is also of type double;
2) As long as one of the two operands is of type float, the other will be Is converted to float type, and the result is also float type;
3) As long as one of the two operands is of type long, the other will be converted to type long, and the result is also of type long;
4) Two operands ( Including byte, short, int, char) will be converted to int type, and the result is also int type.

5. Call by value and call by reference

Call by value means that the method receives the parameter value provided by the caller. Call by reference means that what the method receives is the parameter address provided by the caller. The Java programming language is always called by value. The following is the counter-example code: the
Insert picture description here
above code starts the exchange of the objects referenced by a and b, but the actual compilation and execution will find that the exchange is not successful. This also proves that Java is not called by reference. A and b only represent the value of two Person objects, rather than a reference to two objects. There is no difference in parameter passing from basic types such as int.
Insert picture description here

6. Object initialization

When using the constructor to initialize an object, first run the initialization block, and then run the subject part of the constructor. The specific initialization steps of calling the constructor are as follows:
1) All data fields of the class are initialized to default values ​​(0, false or null).
2) According to the order declared in the class, execute all initialization statements and initialization blocks in turn.
3) If the second constructor is called on the first line of the constructor, the second constructor is executed.
4) The main body of the execution constructor.
When the class is first loaded, the static domain will be initialized. All static initialization statements and static initialization blocks will be performed in the defined order.
The statement that uses super to call the constructor must be the first statement of the subclass constructor.

7. Array

In Java, the reference of the subclass array can be converted to the reference of the parent class array, without the need to use coercive type conversion.

8. Inheritance

When overriding a method, the visibility of the subclass method cannot be lower than that of the parent class method. That is, the parent class method is protected, and the method covered by the subclass can only be protected or public.

9.final modified class

If you declare a class as final, only the methods in it will automatically become final, not including the domain.

10.equals method

The equals method of the Object class is used to detect whether an object is equal to another object, that is, to determine whether the references of two variables are the same. If you redefine the equals method, you must redefine the hashCode method, because when you add data to the hash table, the insertion position will be determined according to the hashCode and equals methods. If x.equals(y) returns true, then the return value of x.hashCode() and y.hashCode() must be the same.

Since the enumeration value has a fixed instance, you can directly use "==" to determine whether the two enumeration values ​​are the same, without using the equals method.
Insert picture description here

11.Class class

The JVM will generate an instance of the Class type for each loaded class, which is used to track the class to which the object belongs. The method of obtaining an instance of the Class type is as follows:
1) The getClass() method in the Object class will return an instance of the Class type.
2) Class.forName(className) can return the Class instance of the class specified by className.
3) MyClass.class can return a Class instance of the MyClass class.

12. Partial class

The class declared in the method is called the local class (also belongs to the inner class), the local class can not only access the external class that contains them, but also access the local variables. However, local variables that can be accessed must be declared as final.

Reader benefits

Thank you for seeing here!
I have compiled a lot of 2021 latest Java interview questions (including answers) and Java study notes here, as shown below
Insert picture description here
Insert picture description here

The answers to the above interview questions are organized into document notes. As well as interviews also compiled some information on some of the manufacturers & interview Zhenti latest 2021 collection (both documenting a small portion of the screenshot) free for everyone to share, in need can click to enter signal: CSDN! Free to share~

If you like this article, please forward it and like it.

Remember to follow me!
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_49527334/article/details/114662288