1. Not the same. The former is "raw data type", variables such as Int,float, the latter is a "reference type" variable, "reference" a variable of an object is called "reference type" variable, sometimes referred to as "object variable". In terms of variables and memory allocations, when declaring a variable of an object type, there is actually no object created, and this variable is =null. When a variable of the original type is defined, it is allocated memory immediately. From the initialization of a variable, Java requires that the variable be explicitly initialized when the original variable is defined, and the initialization of the object variable: If the object variable does not reference a real object, it must be declared null.
2. Object variables can also use "= =" to determine whether two variable values are equal?
public class T {
public static void Main (string[] args) {
Foo obj1=new foo ();
Foo obj2=new foo ();
System.out.println (OBJ1==OBJ2);
}
}
Class foo{
int value=100;
}
public class T {
public static void Main (string[] args) {
Foo obj1=new foo ();
Foo obj2=new foo ();
System.out.println (Obj1.value==obj2.value);
}
}
Class foo{
int value=100;
}
When the "= =" is applied to the original data type variable, is whether the data saved by the comparison variable is equal when "= =" is applied to a reference type variable, compares whether the two variables refer to the same object. The reference represents the address, so "= =" is actually equivalent to comparing the address of the object saved in the two reference type variable
3. How do I compare the contents of a two object?
The "content" of two objects, in fact, refers to the values of all the fields at a certain point in time, "content equal", in fact, the "corresponding field value" is consistent.
In Java, the Equals () method of the base class (override) can be overridden for a field value of two objects
4. Why is the following code not compiled? Where did it go wrong?
public class Test {
public static void Main (string[] args) {
TODO auto-generated Method Stub
Foo obj1 = new Foo ();
}
}
Class foo{
int value;
Public Foo (int initvalue) {
Value=initvalue;
}
}
If a class provides a custom construction method, the default construction method is no longer provided by the system. So you can add "foo obj1 = new Foo ();" Change to "foo obj1 = new Foo (4);".
5. If a class has both an initialization block and a constructor, and it also sets the initial value of the field, who decides?
In "public int field = 100;" In "{field=200;}" Before, it was "{field=200;}" In the end, it is "public int field = 100;" Say That is, who is more dependent upon who initializes the function. Executes the default value specified by the class member definition or the initialization block of the class, exactly which one you want to see in front of. Executes the constructor of the class. The initialization blocks of the class do not receive any arguments, and they are executed whenever an object of the class is created. Therefore, it is appropriate to encapsulate those "code that must be executed when the object is created."
6. Using the class defined on the previous page slide, what is the output of the following code?
The results of the operation are divided into two parts:
① "public int field = 100;" In "{field=200;}" Before
② "public int field = 100;" In "{field=200;}" After
Java field Initialization law: 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 is initialized to false by default;
7. Run the following
Static initialization block execution order: static initialization block of the parent class static initialization block the initialization block of the parent class the constructor of the subclass of the constructor of the parent class that initializes the Block class
8. 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 text3{int x = instance variable of the 3;//class, initializes a static variable with a value of 3 static int y = 4;//class, Initializes a value of 4 public static void Method ()//static methods { System.out.println ("instance variable x =" + New Text3 (). x);//Accessing an instance variable of a class in a static method requires first instantiating the class SYSTEM.OUT.PRINTLN ("static variable y =" + y);//static variable of class can be accessed directly in static method} public static void Main (string[] args) { Text3.method (); TEXT3 ex = new Text3 (); System.out.println ("x =" + ex.x); System.out.println ("y =" + ex.y);} }
9. 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?" ”
public class Text1
{
public static int num;//static field
Public Text1 ()
{
num++;
System.out.printf ("You have created%d objects \ n", num);
}//constructor function
public static void Main (string[] args)
{
num=0;
Text1 a=new Text1 ();
Text1 b=new Text1 ();
Text1 c=new Text1 ();
}
}
Java Chapter III Finishing