Questions in class
1. Early we often define variables like this
int value=100;
The preceding example defines the variable
MyClass obj = new MyClass ();
Are the variables defined in these two ways the same?
A: Not the same, before you define a variable that defines the original data type, and the next one defines an object variable.
2. for variables of the original data type (such as int), you can use "= =" To determine whether two variable values are equal, and the object variable can also use the "= = "is the value of two variables equal?"
not to. Because object variables use "= =" To determine whether an address is equal.
3. Please enter and run the following code to get what results ?
public class test{
public static void Main (String args[]) {
Foo obj1=new foo ();
Foo obj2=new foo ();
System.out.println (OBJ1==OBJ2);
}
}
Class foo{
int value=100;
}
4.
Please summarize, what are the "differences" in this approach, and how many can you list?
A: The function name is the same as the class name and there is no return value.
5. Why is the following code not compiled? Where did it go wrong?
A: The Foo class constructor has parameters, while the main function defines the object without adding a variable.
6. If a class has both an initialization block and a constructor, and it also sets the initial value of the field, who decides?
A : They are all used for initialization, the execution order of the three is the initialization block, the initial value, the construction method, so the final construction method is the last word.
7. What is the result of the following code output?
Answer: The output is
8. According to the output of the code, you should summarize The rules of Java field initialization.
A :When a Java field is initialized when there is an initialization block, an initial value, a constructor in a class, when an object is created with no parameters, the steps for initializing the block and initial values are initialized, and the parameterless constructor is called, and the constructor is called again when the parameter is added. So the first value is three , the second is .
9. Run the teststaticinitializeblock.java Sample, observe the output, and summarize the "Order of execution of static initialization blocks".
Summary: object Initialization Order : First executes the parent class static content, after the parent class static content executes, then executes the subclass static content, when the subclass static content executes completes, then goes to see the parent class has not the non-static code block, if has executes the non-static code block of the parent class, The non-static block of code for the parent class is executed, followed by the constructor of the parent class, and after the construction method of the parent class is executed, it goes on to see if the subclass has no non-static code block, and if there is a non-static block of code that executes the subclass. The non-static code block of the subclass executes and then executes the subclass's construction method. In short, the static code block content executes first, then executes the parent class non-static code block and construction method, then executes the subclass non-static code block and constructs the method.
10.
Two is exactly the same for integers, why is one output trueand one output false?
A:the integer is not a basic type, but a wrapper class for int, so the above indicates that I1 and J1 point to the same object, while i2 and J2 are pointing to different objects, respectively.
When you create an integer object by using 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 I2 in the above code are 100, so the objects that already exist are taken directly from the cache, so i1 and J1 point to the same object, while i2 and J2 point to different objects respectively.
11. Using the class's static fields and constructors, we can track the number of objects created by a class. Write a class that can query it at any time "How many objects have you created?"
20,163,677 Multi-Wen Jia
Public class Duixiang {
Public Static int I= 0;
{
i+=1; additive to create the number of objects
}
Public int Chaxun () {// to query the number of currently created objects
return i;
}
Java Classroom Assignments