1. Why is the following code not compiled? Where did it go wrong?
Because there is already an argument constructor for the Foo class in the Foo class, the parameterless constructor for Foo () is not already in the Foo class, so the constructor cannot be called at New Foo (). So it is impossible to pass the translation. You can then write a parameterless constructor in the Foo class, so that you can compile the new foo ().
2. Using the class defined on the previous page slide, what is the output of the following code?
Results:
Java field initialization: The variables of all classes are initialized to NULL by default, such as String A,integer B; The original data type of the numeric class is initialized to 0 by default, such as int a,short B,char C;boolean default is initialized to False. Java is initialized in two places: initialization blocks and constructors, where initialization blocks are divided into static initialization blocks and instance initialization blocks. Static initialization blocks are initialization blocks in a class that are decorated with static, and instance initialization blocks are initialization statements that do not have any keyword adornments in the class. If there is no formal parameter when creating an object in the main function, if a common variable is defined in the class and assigned, then the value is assigned to the variable in the main function, the default constructor in the class is called, and the corresponding constructor in the class is called if the object is created in the main function as a physical parameter.
3. Run the Teststaticinitializeblock.java sample, observe the output, and summarize the "Order of execution of static initialization blocks".
Results:
All static initialization blocks are performed first, followed by non-static initialization blocks and constructors, which are executed in the following order: Static initialization blocks of the parent class > static initialization blocks of subclasses > initialization blocks of the parent class > Constructors of the parent class > initialization blocks of subclasses > The constructor for the subclass.
4. Static methods only allow access to static data, so how do you access the instance members of the class in a static method (that is, a field or method that does not have the static keyword attached)?
public class A {
public static void Main (string[] args) {
A a=new a ();
System.out.println ("value=" +a.ab ());
}
static int AB () {
A a=new a ();
int Value=a.ac ();
return value;
}
int AC () {
int value=1;
return value;
}
}
Results:
5. Two pairs of integers exactly the same, why one output true, one output false?
Reason:
When Java creates an integer object through the ValueOf method, if the value is between [-128,127], a reference to the object that already exists in Integercache.cache is returned, otherwise a new integer object is created. The values for I1 and J1 are 100, so the objects that already exist are taken directly from the cache, so i1 and I2 point to the same object, while I2 and J2 point to different objects respectively. the "= =" symbol is a comparative memory address when comparing objects, whereas for raw data types it is a direct alignment data value.
Java After class study questions (iii)