Shallow copy and deep copy of Java objects && String type of assignment

Java data types are divided into basic data types and reference data types. For both types of data, performing the assignment, transmission parameters, or a method return value, the value will be passed by reference, and the difference (address) is transmitted.

Shallow copy (Shallow Copy):

① for the data type is the basic data type member variable, shallow copy transfer a value directly, i.e. attribute values ​​of the object to the new copy. Because two different data, to which the member variables of an object to be modified value, another copy of the object will not affect the data obtained.

② data type is a reference for the data type of the member variable, for instance variable is an array of members, such as a class of objects, so shallow copy will be passing references, that is, only the reference value (memory address) member variables copy to the new object. Because in fact the member variables of the two objects point to the same instance. In this case, in a modification of the object member variables affect the values ​​of the member variables of another object.

Note: the equivalent basic type String data type by constant values, that is, reference data types when creating objects through the new keyword

Under the following conditions are shallow copy of the object:

(1) Copy constructor

(2) ordinary rewrite clone () method (using the clone method of class must implement the Cloneable interface, otherwise it will throw an exception CloneNotSupportedException)

Deep copy :

First introduced the concept of the object graph. Imagine a class has an object, its members have a variable object that points to another object, another object, which points to another object, an instance until a determination. This forms the object graph.

For deep copy, copy the object not only to the basic data types of all member variable values, but also to apply storage space for all the member variable references data types, and copies each member variable reference object data type referenced until the object can be All of the objects. In other words, to make a copy of the entire object graph!

Simply put, a deep copy of the object graph referenced data type member variable of all objects are opened up memory space; and shallow copy just point to the delivery address, the new object does not create memory space of reference data types.

Because creating memory and copy the entire object graph, it is compared to a shallow copy deep copy slower and larger expenses.

The following cases are deep copy of an object:

(1) all objects are objects override FIG clone () method to achieve a deep copy: each object of each layer of the object of FIG implement Cloneable interface and override the clone method of the last rewrite of the topmost class All clone method calls the clone method can be realized in a deep copy. Simply put: every object in each layer are shallow copy = deep copy.

(2) achieved by deep copy object serialization

 

Java string constants assigned by the difference between new construction and use of the String object

String str1 = "ABC";
String str2 = new String("ABC");

String str1 = "ABC"; may or may not create a create an object, if the "ABC" string does not exist in java String pool, the pool will be created in a java String to create a String object ( "ABC"), then str1 this points to the memory address, regardless of how many is "ABC" string objects created in the future in this way, always only one memory address is assigned, a copy of string after all, Java called "string resident" All string constants are automatically reside after compilation.

String str2 = new String ( "ABC"); create at least one object, possibly two. Because the new keyword is used, will certainly create a str2 String object in the heap, its value is "ABC". And if this string again java String pool does not exist, create the String object "ABC" in the java pool

Copy the code
String str1 = new String("ABC");
String str2 = new String("ABC");
System.out.println(str1 == str2); //false

String str3 = "ABC";
String str4 = "ABC";
String str5 =  "AB" + "C";
System.out.println(str3 == str4);   //true
System.out.println(str3 == str5);  // true


String a  = "ABC";
String b = "AB";
String c = b + "C";
System.out.println( a == c );//false
Copy the code

Note that a final judgment and equal to c: a, b at compile time has been determined, and c is the reference variable, is not to be determined at compile time. B runtime and "C" stitching is achieved through StringBuilder (before JDK1.5 is StringBuffer), StringBuilder last call of toString function returns a new String object

Application of the case: the usual recommended use, make use String = "abcd"; in this way to create a string instead String = new String ( "abcd"); in this form, since the use of new constructor creates characters string object will open up a new heap space, double quotes is the use of string intern (string resides) optimized efficiency than constructor.

Guess you like

Origin www.cnblogs.com/nangonghui/p/11593569.html