Please enter and run the following code to get what results ?
Procedures and Results:
Two, why the following code can not be compiled? Where did it go wrong?
Procedures and Results:
Conclusion: If a class provides a custom construction method, the default construction method is no longer provided by the system.
Third, according to the output of the Code, self-summary Java field initialization law.
Program:
Results:
rule: When Java initializes the same variable many times when it is initialized, Java assigns the last initialization result to the variable.
When there is an inheritance relationship between multiple classes, creating a subclass object causes the parent class to initialize the execution of the block.
Program:
Results:
Summarize:
Creating a Subclass object causes the parent class to initialize the blocks in the order that they are executed:
Initializes the static initialization block of the parent class (the highest level), the parameterless constructor of the parent class, the static initialization block of the subclass, and the parameterless constructor of the subclass;
Then is the normal initialization block of the parent class, the normal initialization block of the subclass, the parameter constructor of the parent class, and the parameter constructor of the subclass.
The order in which static initialization blocks are executed is:
Initializes the static initialization block of the parent class (the highest level), the parameterless constructor of the parent class, the static initialization block of the subclass, and the parameterless constructor of the subclass;
Then is the normal initialization block of the parent class, the normal initialization block of the subclass, the parameter constructor of the parent class, and the parameter constructor of the subclass.
Static initialization blocks are executed only once. When you create an object of a subtype, it also causes the execution of the static initialization block of the parent type.
V: accessing instance members of a class in a static method
Program:
Six: " Magic code ",Integer " weird " features
Program:
Results:
Cause: It calls the valueOf method in the Integer as follows:
The above code shows that when the value is-128~127, it returns a pointer to theIntegercache.cachea reference to an object that already exists in the, otherwise creates a newIntegerobject. BecauseI1and theI2the value is -, so it will be directly fromCachetakes an already existing object, soI1and theI2point to the same object, andi3and theI4it points to different objects, respectively.
Java Classes and objects