Java Language Basics

Question 1: What is the most efficient way to figure out 2 times 16?

1-1 Questions and answers

In Java programs, the value of 2 times 16 can be computed in a number of ways, for example, using 2*16 or using 2<<4. So what's the most efficient way to figure out 2 times 16? (1) Since bit operations are the most efficient in a computer, we should start with bit operations if we want to find the most efficient operation method. (2) The Java language's bitwise operations provide left shift, right shift, and unsigned right shift operators, which are <<, >>, >>> respectively. (3) The left shift makes the operand larger. It is equivalent to multiplying the left operand by 2 to the NTH power, which is determined by the right operand. (4) On the contrary, the right shift operation implements division. Use the bitwise operator to calculate 2 times 16 as follows: intValue =2<<4;Copy the code

1 to 2

The code snippet above 2 < < 4 is to shift to the left 2 and 4 bit operations, equivalent to two times 4 power of 2, performing after the value of value is 32, with 2 x 16 values are the same, but because of the speed of the bit operations to be faster than the speed of multiplication, so 2 x 16 a few the most efficient calculation method should be written as 2 < < 4.Copy the code

Question 2. How do I use the bitwise operator to compute 2 to the NTH power?

Questions and Answers

At work, it is sometimes necessary to compute two to the NTH power. The more times you do this, the more likely it is to make mistakes. For example, it is easy to multiply or multiply two to the twentieth power. So how do you use the bitwise operator in a program to compute 2 to the twentieth power? int result=1<<20;Copy the code

Question 3. What is the difference between Final, finally and Finalize?

3-1 final

(1) Final represents a modifier that, if used to modify a class, is not inherited; (2) If you use it to modify a variable, the variable cannot be modified once assigned; (3) If you use it to modify a method, that method cannot be overridden.Copy the code

3-2 final

Finally is used for exception handling, which decorates a code block that executes even if the previous code handles exceptions. Usually used to release resources.Copy the code

3-3 finalize

Finalize represents a method defined in the Object class - a method that can be overridden to reclaim resources.Copy the code

3-4

Although these three words are very similar, they are used in very different ways. (1) Final is a modifier that means it cannot be changed. (2) Finally is used in exception catching to indicate the code that must be executed. (3) Finalize means that a method name is related to garbage collection.Copy the code

Question 4. Question 89, 90, 91, 92

4-1

Question 8. What is required of equals() in the Java language specification?

8-1 Questions and answers

In the Java language, developers are allowed to override the equals0 method. However, rewriting the method must comply with the Java language specification. What are the requirements for equals0 in the Java language specification? In the Java language specification, equals() is required to follow the following convention: (1) reflexivity: for any non-null reference value x, x.equals(x) should return true. (2) Symmetry: for any non-null reference values x and y, x. quals(y) should return true if and only if y.quals (x) returns true. (3) Transitivity: for any non-null reference values x, y, and z, if x.quals (y) returns true and y.quals (z) returns true, then x.quals (z) should return true. (4) Consistency: Multiple calls to x.equals(y) always return true or always return false for any non-null reference values x and y, provided that the information used in the equals comparison on the object has not been modified. (5) for any non-null reference value x, x. beers (null) should return false.Copy the code

8-1 Supplementary notes

The Java language specification requires equals0 methods to be reflexive, symmetric, transitive, consistent, and so on. When you override the equalsQ method, you usually also need to override the hashCode() method.Copy the code

100