Zi Defining classes
Boxandunbox. Java
Hello. Java
Initializeblockclass. Java
That is, only the definition of the member variable, not the active initialization operation, the compiler assigns a default value to the member variable.
For basic types, there are different default values such as int, char, short, and so on, 0,boolean to false, and null for the object. The default initialization is to ensure that the initialization process will be performed, only on member variables, and there is no such mechanism for local variables.
Mypackageclass. Java
ObjectEquals
Strangeintegerbehavior . Java
for two non- new integer objects, when compared, if the value of two variables is between 128 and 127, the comparison is true, and if the value of the two variable is not in this interval, the result of the comparison is false
The result is false when a non- new generated integer variable is compared to a variable generated by the new Integer (). (Because a non-new integer variable points to an object in a Java constant pool, the new Integer () generates a variable that points to the new object in the heap, and the address in memory is different)
Teststaticinitializeblock. Java
In a class
class content (static variable, static initialization block) = = instance content (variables, initialization blocks, constructors)
In two inheritance classes
parent class (static variable, static initialization block)= = Subclass (static variable, static initialization block)= = parent class (variable, initialization block, constructor)= = subclasses (variables, initialization blocks, constructors)
Test2
Access class strength members in static methods
Package main;
Public class Circle {
Private Double radius = 1.0;
double Getare () {
return radius * radius * Math. PI;
}
Public Static void Main (string[] args) {
Circle mycircle = new circle ();
System. out. println ("radius is" + Mycircle.radius); Radius is not static, static methods can call non-static member variables, non-static methods through class objects
}
}
Packagemain; Public classCircle {Private DoubleRADIUS = 1.0; DoubleGetare () {returnRADIUS * RADIUS *Math.PI; } Public Static voidMain (string[] args) {Circle mycircle=NewCircle (); System.out.println ("Radius is" + Mycircle.radius);//radius is not static, static methods can call non-static member variables, non-static methods through class objects }}
10-21 hands on the brain