1. What is the difference between an object and an object reference?Please give an example
We often see:
plane fp=new plane();
Here, new plane () creates an object, and fp is called a reference to that object.
Specifically, there are several steps
plane fp; //Opens up space in stack memory for the reference variable fp, where fp=null fp=new plane(); // new plane() opens up space in heap memory for an object of the plane class that has no name // plane() then calls the planeclass constructor // Give the reference variable plane the address of the object in heap memory
Note: In this case,'='means to assign the object's address to the reference fp. That is, what is stored in FP is actually an object's address, which can be manipulated by fp.
Image point, like the relationship between remote control (object reference) and TV (object)
Supplement: An object reference variable takes up 4 bytes of space; an object takes up at least 16 bytes, which contains 12 bytes of object header information. In addition, there is instance data to align the fill bytes.
2. What are the characteristics of object passing as a parameter? Please give an example.
First, let's make it clear that basic data types pass as parameters are value transfers
public class ValuePass { private static int a; public static void main(String [] args) { modify(a); System.out.println(a); } public static void modify(int a) { a++; } } //0
Passing objects as parameters is referential, that is, passing object addresses
class IntClass { int value; } public class RunIntClass { public static void modifyValue(IntClass s, int val){ s.value = val; } public static void main(String[] args) { IntClass a = new IntClass(); modifyValue(a,8); System.out.println(a.value); } } //8
class IntClass { int value; } public class RunIntClass { public static IntClass getInstance() { //Generate objects in methods IntClass s = new IntClass (); s.value=8; return s; //Return Object Reference } public static void main(String [] args) { IntClass a = getInstance(); System.out.println(a.value); } } //8
3. What is the order in which objects are initialized?
Initialization order when creating objects:
Data members are initialized by default by the system
> Execute the initialization statement at the data member definition
> Call the construction method to specify an initial value for the data member
class IntClass{ int value;//The default value for automatic initialization is 0 //int value=5;Specify initial value when defining } public IntClass(){} public IntClass(){ value=val; } public class IntClassConstructor{ public static IntClass getinstance(){ IntClass s=new IntClass(8); return s; } public static void main(String args[]){ IntClass a=new IntClass(); System.out.println(a.value); a=getinstance(); System.out.println(a.value); } }//0 8
4. What is the difference between the static field and the non-static field of a class? What scenarios should the static modifier be used? Please give examples
> Data members decorated with static modifiers are not specific objects of any class but static data members of the class.
> It is stored in the public storage unit in the memory area of a class, not in the memory area of an object. Therefore, when any object of a class accesses it, it accesses the same value.
Is accessed by the class name plus a dotted operator or by object references.
>Static data members are initialized only when the class is loaded and only once
Code inside the static method can only access the static attribute or method in the class, not the non-static attribute or method in the class (because that is the object method), but the non-static method can access the static data member.
The main method, a special static method, is the entry point for the Application program and must be written as a public static void main(String [] args).
Output 5 2
Static Code Block
Static code blocks that are not included in any method body can be used in a class. When a class is loaded, static code blocks are executed only once and are often used to initialize properties defined in a class.
class Test { static int value ; static { value = 3; System.out.println("value="+value); } public static void main(String[] args){ } } //Program output value=3