Common myths and details in Java

Source: Internet
Author: User
Tags goto

I stayed in the book the whole day yesterday. I was going to find some books to learn "regular expression", very disappointed, did not find this part of the content of the book. Found a "Java in-depth analysis," which involves a lot of the usual unnoticed some of the misunderstanding, perhaps the development of the time is not used, but these concepts can not be blurred. The contents of the book are still useful, and some notes are summarized here.

1 in Java, there are no goto statements. The use of the Java language is canceled because a large number of goto statements can reduce the readability and maintainability of the program goto . At the same time, in order to avoid the goto confusion caused by the programmer's own use, the Java language will still be goto defined as a keyword, but no syntax is defined, so it is called "reserved word".

2 true , false and null in the IDE although a different color display, but not the keyword, but "literal constant", just String like the type abc .

3 define the name as much as possible $ , as the compiler compiles "$" into a connector of the top-level type and the underlying type when compiling the. java file. 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!");
   }
 }
}

When compiling ( javac Test3.java ) This code, the compiler will report the following error:Test.java:12: 错误: 类重复: com.laixintao.Test.Outer.Inner class Inner{ ^

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

// char c1 = ‘u00a‘;
// char c2 = ‘u00d‘;

These two lines of code compilation error occurred in the program. These two Unicode codes represent "line break" and "carriage return", so the code is like this when the compiler compiles:

// char c1 = ‘
‘;
// char c2 = ‘
‘;

5 Unicode codes are encoded using 16-bit characters and are represented in Java by char type. Now Unicode has been extended to 1 million characters, which exceeds the 16-bit limit to become supplementary characters. all supplemental characters cannot be represented by character constants.

6 when, when short byte char participating in an operation, the result is int type, not the same as the higher type. If the variable is, byte short byte type, when it is given a compile-time constant, and the constant does not exceed the value range of the variable, the compiler can make an implicit narrowing conversion. This implicit narrowing conversion is safe because the narrowing conversion applies only to the assignment of the variable, not to the method invocation statement, which is not used for parameter passing at the time of the method invocation. (see small problems with default type conversions in Java)

7 Note The char type, which is an unsigned type. Therefore, char the short conversion with or char byte between must be used to display the type conversion. The transition from byte to to char an extended narrowing transformation, which is special, is to first byte convert the extension to int , and then shrink to char .

8 in an extended conversion between integer data, if the operand is of char type (unsigned type), an unsigned extension is made, and the extension bit is 0. If the operand is byte , short or int (signed), the symbol extension is made, and the extension bit is the symbol bit of the variable.

9 Narrowing conversions between integer data are simply truncated and dropped high, without any other processing.

Ten 0.1+0.2 is not equal to 0.3. System.out.println((double)0.1+(double)0.2); The output of this statement is 0.30000000000000004 . Because computers use binary to store data, many decimals do not accurately use binary notation (in fact, most decimal places are approximate), just as decimal decimals do not accurately represent 1/3 of such fractions. Most floating-point types store their values only approximately in the computer, rather than exactly as integral types. Another example of this is a dead loop:for(float f = 10.1f;f != 11;f+=0.1f){}

A float type can retain 7~8 valid digits, and a double type can retain a valid number of 15~16, so that some of the least significant bits of the value are lost when the int or long value exceeds a valid number of double or float, resulting in loss of precision, , the IEEE754 most recent rounding mode is used to extract the floating-point value closest to the integer value. Although integer-to-float conversions are extended conversions, when the values are large or small (with a large absolute value), a certain degree of precision loss occurs.

i+++jhow to calculate? (This problem is not much of a concern in C/s + +), because C + + relies on the hardware structure of the implementation, and different environmental outcomes are different. In Java, however, the result is fixed, unaffected by the hardware environment and platform it is running on. A: According to the greedy rule, the front + + + is better than the Post + +, the result is(i++)+j

i++ and ++i are actually all first +1, and then the value of the assignment. ++i, there is nothing to say; i++, as j=i++; An example at the bottom of the implementation is: temp = i;i = i + 1; j = temp; So, the i=15;i=i++; result of this expression is 15. (Since the addition of one after another assignment, from 16 to 15)

14 +0 and 0 in the floating-point type variable store, the sign bit is different. When 0 and +0 participate in related operations of floating-point types, such as division and redundancy operations, different results can be produced.

15 The division of floating-point and the remainder operation are different from the integer type and the remainder operation, when the divisor is 0 o'clock, the floating-point operation will not produce an ArithmeticException exception.

StringA class is a non-mutable class whose objects are not destroyed once they are created. Stringclasses that appear to modify a sequence of characters actually return the newly created object instead of String modifying the object itself.

17 because the String object is immutable, it has thread safety and is free to share.

18 String inside the class, a character array () is used char[] to maintain the character sequence. The maximum length is the maximum length of the String character array, theoretically the maximum length is the maximum value of type int, or 2147483647. In practice, the generally available maximum is less than the theoretical maximum value.

main()the method behaves in the same way as other methods, which can be overloaded, called by other methods, inherited, hidden, and can throw exceptions with type parameters. We can also invoke main方法 (or other methods) in a program by reflection.

20 These methods form an overload when two or more methods have the same name, and the argument list is not the same. Overloaded methods can be distinguished by the number of arguments corresponding to the parameter list, but the name of the parameter, the return type of the method, the exception list of the method, and the type parameter cannot be used as a criterion for distinguishing between overloaded methods.

21 exactly which method call to choose, the order is this:

    • In the first stage, the automatic packing (unpacking) and the variable parameters are not considered, the search corresponding parameter type can match the actual parameter type and the number of parameters and the number of arguments is the same method;

    • If there is no qualifying method in step one, the automatic boxing and unpacking will be performed in the second stage.

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

    • If none of the 3 phases are searched for a qualifying method, a compilation error will occur. If you have more than one method for the condition, you will choose the most explicit method. The most explicit method is defined as: If the parameter list type of a method can be assigned to the parameter list type of the B method, then the a method is more explicit than the B method. If the most explicit method cannot be selected, a compilation error is generated.

22 The essential difference between rewriting and hiding is that overrides are dynamically bound, depending on the actual type of object that the runtime reference points to to invoke the members of the related class. While hiding is statically bound, the associated member of the call is determined based on the static type referenced at compile time. In other words, if a subclass overrides a method of a parent class, a subclass method is called by reference to the parent class when the parent class's reference points to the child class object. If a child class hides a method (member variable) of the parent class, the parent class's method (member variable) is called by reference to the parent class.

The 23 constructor is called recursively, and the constructor of the child class invokes the constructor of the parent class until the constructor of the object class is called.

The 24 constructor does not create an object, which is called by the system when creating an object with new, and is used to initialize the instance members of the class. In order, the object is created first, and then the constructor is called. (the constructor does not produce a new object)

25 The default constructor is not empty, and the constructor invokes the parameterless constructor of the parent class and may perform initialization of the instance member variable. So, the default constructor calls at least the constructor of the parent class, and it can do more work, including instance variable declaration initialization and instance initialization blocks, all executed in the constructor.

26 when the = = or! = Operator's two operand type one is the base data type, the other is the wrapper class reference type, the reference type is disassembled into the base data type, and then the values of the two base data types are compared for equality.

27 in Java, an array is also a class, and the reference variable for an array declaration points to an object of the array type. All arrays inherit the class and are Object implemented with the java.lang.Cloneable java.io.Serializable interface. The members of an array include variables length (implicitly present) and members inherited from the object class. Cloneablewith Serializable an interface that is two tokens, no members are explicitly declared in both interfaces.

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

29 If the two interfaces declare the same variable x, a compile error is generated by simple name access when an interface inherits both interfaces, or if a class implements both interfaces at the same time.

30 if a method m of the same name is declared in two interfaces, and two methods do not constitute an overload, if an interface can inherit both interfaces at the same time, or if a class can inherit both interfaces at the same time, there must be a method signature that causes the signature to be a sub-signature of two m method signatures, and on the return type of the method , there must be a type that enables the type to return a replaceable type of type for two m methods at the same time.

Common myths and details in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.