How Java object objects are created [reproduced]
@author http://blog.csdn.net/mhmyqn/article/details/7943411
Explicitly create an
There are 4 ways to explicitly create an object:
1. Constructor : Create an object with the new statement, which is the most common way to create an object.
2. Reflection : Using the Reflection method, call the Java.lang.Class or Java.lang.reflect.Constructor class newinstance () instance methods.
3. Clone : Invokes the object's clone () method.
4. Serialization : Invoke the ReadObject () method of the Java.io.ObjectInputStream object using the deserialization means.
The following illustrates the process of creating an object in the previous 3 ways.
1 Public classCustomerImplementscloneable{2 PrivateString name; 3 Private intAge ; 4 PublicCustomer () {5 This("Unknown", 0); 6SYSTEM.OUT.PRINTLN ("Call default constructor"); 7 } 8 PublicCustomer (String name,intAge ) { 9 This. name=name; Ten This. age=Age ; OneSystem.out.println ("Call second constructor"); A } - PublicObject Clone ()throwsclonenotsupportedexception{ - return Super. Clone (); the } - Public Booleanequals (Object o) { - if( This==o)return true; - if(! (OinstanceofCustomer))return false; + FinalCustomer other=(Customer) o; - if( This. Name.equals (Other.name) && This. age==other.age) + return true; A Else at return false; - } - PublicString toString () { - return"Name=" +name+ ", age=" +Age ; - } - Public Static voidMain (String args[])throwsexception{ in //using reflection to create a customer object -Class objclass=class.forname ("Customer"); toCustomer c1= (Customer) objclass.newinstance ();//invokes the default construction method of the Customer class +SYSTEM.OUT.PRINTLN ("C1:" +C1);//Print name=unknown,age=0 - the //Create a Customer object with the new statement *Customer c2=NewCustomer ("Tom", 20); $System.out.println ("C2:" +C2);//Print name=tom,age=20Panax Notoginseng - //creating a Customer object using cloning theCustomer c3= (Customer) C2.clone ();//the construction method of the customer class is not called +System.out.println ("C2==C3:" + (C2==C3));//Print False ASystem.out.println ("C2.equals (C3):" +c2.equals (C3));//Print True theSystem.out.println ("C3:" +C3);//Print name=tom,age=20 + } -}
The printing results of the above program are as follows:
Call second constructor
Call default constructor
C1:name=unknown,age=0
Call second constructor
C2:name=tom,age=20
C2==c3:false
C2.equals (C3): True
C3:name=tom,age=20
As seen from the above printout, when creating a Customer object with the Newinstance () method of the new statement or class object, the method of constructing the customer class is executed , and the object's clone () method does not perform the construction of the customer class when the customer object is created. (difference)
Implicitly-Created
You can also implicitly create objects in your program, including the following 4 scenarios:
1. Command-line arguments: for each command-line argument in the Java command, the Java virtual machine creates the corresponding string object, organizes it into a string array, and passes the array as a parameter to the program entry main (String args[]) Method.
2.String Type: the direct number of type string in the program code corresponds to a String object, for example:
1 String s1= "Hello"; 2 // S2 and S1 reference the same string object 3 string s3=new string ("Hello"); 4 // Print True 5 // Print False
After executing the above program, there are actually only two string objects in memory, one is the direct number, implicitly created by the Java Virtual machine, and one is explicitly created by the new statement.
3. String concatenation: The operation result of the string operator "+" is a new string object. For example:
1 String s1= "H"; 2 String s2= "Ello"; 3 // S3 refers to a new string object 4 // Print False 5 // Print True
4. Bytecode Generation class object: When a Java virtual machine loads a class, it implicitly creates a class instance that describes the class.
How Java object objects are created [reproduced]