Home > Java > javaTutorial > body text

Common misunderstandings and details of Java

黄舟
Release: 2017-02-06 15:56:01
Original
1181 people have browsed it

I found a book called "Java In-depth Analysis", which involves many misunderstandings that I usually don't pay attention to. Maybe I won't use them during development, but I can't be vague about these concepts. The content in the book is still very useful. Here are some summary notes.

Common misunderstandings and details of Java

#1. In Java, there is no goto statement. Because extensive use of goto statements will reduce the readability and maintainability of the program, the use of goto has been canceled in the Java language. At the same time, in order to avoid the confusion caused by programmers using goto on their own, the Java language still defines goto as a keyword, but does not define any syntax, so it is called a "reserved word".

2 Although true, false and null are displayed in different colors in the IDE, they are not keywords, but "literal constants", just like abc of String type.

3. Try to avoid using $ when defining the name, because when the compiler compiles the .java file, it will compile "$" into a connector between the top type and the bottom type. See the following example:

package com.laixintao.Test;   

public class Outer$Inner {   
    public static void main(String[] args) {   
        Outer o = new Outer();   
        Outer.Inner i = o.new Inner();   
        i.innerPrint();   
    }   
}   

class Outer {   
    class Inner {   
        void innerPrint() {   
            System.out.println("Inner Print!");   
        }   
    }   
}
Copy after login

When compiling (javac Test3.java) this code, the compiler will report the following error: Test.java:12: Error: Duplicate class: com.laixintao.Test.Outer .Inner class Inner{ ^

4. Unicode escape characters are processed very early, before the parsing program. For example:

// char c1 = 'u00a';   
// char c2 = 'u00d';
Copy after login

When these two lines of code appear in the program, a compilation error is reported. These two Unicode codes represent "line feed" and "carriage return" respectively. Therefore, when the compiler compiles, the code looks like this:

// char c1 = '   
';   
// char c2 = '   
';
Copy after login

5. The Unicode code uses 16-bit character encoding. In Java, it is represented by the char type. Now Unicode has been extended to one million characters, and those exceeding the 16-bit limit become supplementary characters. All supplementary characters cannot be represented by character constants.

6. When short, byte, and char participate in the operation, the result is int type, not the same as the higher type. If a variable is of type byte, short, or byte, and a compile-time constant is assigned to it, and the constant does not exceed the value range of the variable, the compiler can perform implicit contraction conversion. This implicit shrinkage conversion is safe because the shrinkage conversion only applies to variable assignments and does not apply to method call statements, that is, it does not apply to parameter passing when calling methods. (See the minor issues with default type conversion in java for details)

7. Pay attention to the char type, which is an unsigned type. Therefore, conversion between char and short or char and byte must explicitly use type conversion. The conversion from byte to char is an expansion and contraction conversion. This conversion is quite special, that is, the byte is first expanded and converted to int, and then contracted to char.

8. In the extension conversion between integer data, if the operand is char type (unsigned type), unsigned extension is performed, and the extension bit is 0. If the operand is byte, short or int (signed type), sign extension is performed, and the extension bit is the sign bit of the variable.

9. The shrinking conversion between integer data only truncates and discards the high bits without any other processing.

10. 0.1+0.2 is not equal to 0.3.System.out.println((double)0.1+(double)0.2); The output result of this statement is 0.30000000000000004. Because computers use binary to store data, many decimals cannot be accurately represented using binary (in fact, most decimals are approximate), just like using decimal decimals cannot accurately represent fractions like 1/3 . Most floating-point types only store their values ​​approximately in the computer, not as accurately as integers. Another example, this is an infinite loop: for(float f = 10.1f;f != 11;f+=0.1f){}

11. The float type can retain 7 to 8 significant digits, while the double type The type can retain 15 to 16 significant digits. Therefore, when the int type or long type value has more significant digits than the double or float, some of the least significant bits of the value will be lost, resulting in a loss of precision. At this time, the IEEE754 nearest rounding mode, extracts the floating point value closest to the integer value. Although the conversion of integer to floating point is an extended conversion, when the numerical value is very large or very small (the absolute value is very large), a certain loss of precision will occur.

12. How to calculate i+++j? (This issue is discussed in C/C++) does not make much sense, because C/C++ depends on the implemented hardware structure, and the results will be different in different environments. However, in Java, this result is fixed and is not affected by the hardware environment and platform it runs on) Answer: According to the greedy rule, prefix ++ is better than postfix ++, and the result is (i++) + j

13. i++ and ++i actually add +1 first and then assign the value. ++i, there is nothing to say; i++, taking j=i++; as an example, the underlying implementation is: temp = i;i = i + 1; j = temp; Therefore, the expression i=15;i=i++; The result is 15. (Because an assignment is performed after adding one, changing from 16 back to 15)

14. The sign bits of +0 and -0 in floating point type variable storage are different. When -0 and +0 participate in floating-point type related operations (such as division and remainder operations), different results can be produced.

15. The division and remainder operations of floating point are different from the division and remainder operations of integers. When the divisor is 0, floating point operations will not generate ArithmeticException.

16. The String class is a non-mutable class. Once its object is created, it cannot be destroyed. Those methods of the String class that appear to modify character sequences actually return newly created String objects instead of modifying the object itself.

17. Since the String object is immutable, it is thread-safe and can be freely shared.

18 Inside the String class, a character array (char[]) is used to maintain the character sequence. The maximum length of String is the maximum length of the character array. Theoretically, the maximum length is the maximum value of the int type, which is 2147483647. In practice, the maximum value that can be obtained is generally smaller than the theoretical maximum value.

19. The main() method is basically the same as other methods in terms of performance behavior. It can be overloaded, called by other methods, inherited, hidden, and can also throw exceptions with type parameters. We can also call the main method (or other methods) through reflection in a program.

20. When two or more methods have the same name but different parameter lists, these methods constitute overloading. Overloaded methods can be distinguished based on the type corresponding to the parameter list and the number of parameters. However, the name of the parameter, the return type of the method, the exception list of the method, and the type parameters cannot be used as conditions for distinguishing overloaded methods.

21. Which method to call, the order is as follows:

In the first stage, automatic boxing (unboxing) and variable parameters are not considered, and the corresponding formal parameter type is searched. Methods that can match the actual parameter type and the number of formal parameters is the same as the number of actual parameters;

If there is no qualified method in step one, in the second stage, automatic boxing and unboxing will be executed .

If there is no qualified method in step two, in the third stage, the variable parameter method will be considered.

If no method that meets the conditions is found in the three stages, a compilation error will occur. If there is more than one way to condition, the most specific method will be chosen. The most clear method definition is: if the corresponding formal parameter list type of method A can be assigned to the formal parameter list type of method B, then method A is clearer than method B. If the most unambiguous method cannot be selected, a compilation error will occur.


22 The essential difference between rewriting and hiding is: rewriting is dynamically bound, and the method of calling related classes is determined based on the actual type of the object pointed to by the runtime reference. member. Hidden is statically bound, and the relevant members to be called are determined based on the static type referenced at compile time. In other words, if a subclass overrides a parent class method, when the parent class reference points to the subclass object, the subclass method is called through the parent class reference. If a subclass hides a method (member variable) of the parent class, the method (member variable) of the parent class is still called through a reference to the parent class.

23. The constructor is called recursively. The constructor of the subclass will call the constructor of the parent class until the constructor of the Object class is called.

24 The constructor does not create an object. The constructor is called by the system when using new to create an object and is used to initialize the instance members of the class. In terms of sequence, the object is created first and then the constructor is called. (The constructor does not generate a new object)

25. The default constructor is not empty. This constructor will call the parameterless constructor of the parent class and may perform initialization of instance member variables. Therefore, the default constructor at least calls the constructor of the parent class, and it may do more work, including instance variable declaration initialization and instance initialization block, which are all executed in the constructor.

26 When one of the two operands of the == or != operator is a basic data type and the other is a wrapper class reference type, unbox the reference type and convert it to the basic data type, and then compare Whether the values ​​of two basic data types are equal.

27. In Java, arrays are also classes, and the reference variables declared in the array point to objects of the array type. All arrays inherit the Object class and implement the java.lang.Cloneable and java.io.Serializable interfaces. The members of the array include the variable length (which exists implicitly) and members inherited from the Object class. Cloneable and Serializable are two marked interfaces that do not explicitly declare any members.

28. The interface is a completely abstract design and cannot be instantiated. Using the excuse type created by A using the new method actually creates an anonymous class that implements the interface type.

29. If two interfaces declare the same variable

30. If two interfaces declare methods m with the same name, and the two methods do not constitute overloading, then when an interface can inherit both interfaces at the same time, or a class can inherit both interface, there must be a method signature such that the signature is a sub-signature of two m method signatures at the same time, and in the return type of the method, there must be a type such that the type is the return type of both m methods at the same time replaceable type.

The above are the common misunderstandings and details of Java. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!