A brief summary of the characteristics of strings in java and their extended knowledge

1 : String instance (this website is similar to the explanation http://m.iask.sina.com.cn/b/6gEBjgQchkf.html )

String str1=new String("11"); String str2="11"; at this time str1==str2  is false

Because str1 is an object saved, and the string "11" is not put into the string pool at this time,

str2 first uses the equals method to find in the string pool, if it can't find it, it will create an object in the heap memory and then put the object in the strings pool . If the object is not used, it will be destroyed by the jvm at any time.

String str3 = new String("12"); String str4 = str3.intern(); at this time str3 == str4 is false

Because the object pointed to by str3 , the value is put into the string pool through the intern method and a new address is allocated, so the two values ​​are different;

String str5 = new String("13"); String str6 = str5.intern(); System.gc();

Note: System.gc does not think that the object pointed to by str5 is a garbage object at this time, so it can still output the value of the object pointed to by str5 is 13 (only when the object is no longer referenced will it be considered a garbage object, There are two situations in which jvm triggers gc : 1 : when there is no thread running, 2 : it is triggered when memory is insufficient)

2 : Strings are immutable classes ( refer to https://zhidao.baidu.com/question/1758363222430507468.html)

Conditions that need to be met to create an immutable class

a: declare the class as final , so it cannot be inherited;

b: declare all members as private, so that direct access to these members is not allowed;

c: Do not provide setter methods for variables ;

d: declare all mutable members final so that they can only be assigned once;

e: Initialize all members through the constructor and perform a deep copy ( deep copy )

f: In the getter method, do not directly return the object itself, but clone the object and return a copy of the object;

3 : Extended deep clone and shallow clone

illustrate

The cloning of java objects is a relatively advanced technology. If the members of the class contain variable reference types, deep cloning needs to be used in cloning, otherwise the cloning cannot be completely realized. At the same time, if the reference type is a variable reference type member variable, it also needs to be cloned;

Cloning conditions

a: You must implement the Cloneable interface to be eligible to call the Object.clone() method b: Implementing the Cloneable interface is only one of the conditions for cloning. To be cloneable, you must also rewrite the clone() method of Object   
 

c: (from: https://blog.csdn.net/javafuns/article/details/1746509 )


Clone instance one: https://www.cnblogs.com/acode/p/6306887.html

Clone example 2: https://jingyan.baidu.com/article/4853e1e557f84a1908f72673.html )


4 Accessories

package pri.lsx.test.string;


/**Learn the characteristics of string: from http://m.iask.sina.com.cn/b/6gEBjgQchkf.html
 *
 *Extended to the following knowledge points
 * 1: String is an immutable class (refer to https://zhidao.baidu.com/question/1758363222430507468.html), the following is a satisfying way to create an immutable object
 * a: declare the class as final, so it cannot be inherited;
        b: declare all members as private, so that direct access to these members is not allowed;
        c: Do not provide setter methods for variables;
        d: declare all mutable members final so that they can only be assigned once;
        e: Initialize all members through the constructor and perform deep copy (deep copy and shallow copy can refer to https://www.cnblogs.com/acode/p/6306887.html, and the clone of java object is a It is a relatively advanced technology. If the members of the class contain variable reference types, deep cloning needs to be used in cloning, otherwise the cloning cannot be fully realized. At the same time, if the reference type is a variable reference type member variable of CNOOC, it will Cloning is also required (https://jingyan.baidu.com/article/4853e1e557f84a1908f72673.html);
        f: In the getter method, do not directly return the object itself, but clone the object and return a copy of the object;
 * */
public class StringFeatures {
    public static void main(String[] args) throws CloneNotSupportedException {
        String str1=new String("11"); String str2="11";
        if (str1 != str2) System.out.println("str1==str2 is false");//str1 is the value of an object saved, str2 first goes to the strings pool to find it, if it is not found, it will be in the heap Create an object in memory and put the object in the strings pool
        System.out.println(str1.hashCode());System.out.println("11".hashCode());System.out.println(str2.hashCode());

        String str3 = new String("12"); String str4 = str3.intern();
        if (str3 != str4) System.out.println("str3 == str4 is false"); //The object pointed to by str3 puts the value into the string pool through the intern method and assigns a new address, so this The two values ​​are not the same;

        String str5 = new String("13"); String str6 = str5.intern(); System.gc();
        System.out.println(str5);// System.gc does not think that the object pointed to by str5 is a garbage object at this time, so the value of str5 can still be output at this time (only when the object is no longer referenced will it be considered as a garbage object Garbage objects, jvm triggers gc in two cases: 1: when there is no thread running, 2: it will trigger when there is insufficient memory)

        String str7="14"; String str8="14";
        if (str7==str8) System.out.println("str7==str8 is equal");//The object pointed to by str8 is obtained in the string pool, so it is the same one.

        //Use the clone method, deep clone and shallow clone (Reference: https://www.cnblogs.com/acode/p/6306887.html )
        CloneClass cloneClass = new CloneClass();
        cloneClass.var1 = 10;
        CloneClass cloneClass1=(CloneClass)cloneClass.clone();
        System.out.println("var1="+cloneClass1.var1+";vr2="+cloneClass1.var2+";var3="+cloneClass1.var3);//The member variables in the clone object and the members of the new object The value of the variable is not the same
    }
}

/**
 * Cloning conditions:
 * a: Must implement the Cloneable interface to be eligible to call the Object.clone() method
 * b: Implementing the Cloneable interface is only one of the conditions for cloning. To be cloneable, the clone() method of Object must also be rewritten, because the Object.clone() method is protected and must be overridden and changed to public.
 *c: (from: https://blog.csdn.net/javafuns/article/details/1746509)
 *Note when cloning:
 * a: If the member variables in the cloned object refer to objects of other classes, the object is deeply cloned.
 * b: For reference of deep clone and shallow clone (https://www.cnblogs.com/acode/p/6306887.html, https://jingyan.baidu.com/article/4853e1e557f84a1908f72673.html)
 *
 *Extension:
 * 1: The Java virtual machine automatically initializes the default values ​​of member variables, the default initial values ​​of the following types.
     a. The default value of a boolean basic type variable is false.
     b. The variable of reference type is the default value of null. Such as: BigDecimal, String, Integer, Boolean, etc.
     c. The default value of a variable of an array reference type is null. When an array variable is instantiated, Java initializes all elements of the array to their default values ​​of the corresponding type if there is no explicit assignment to each element.
     d. The default value of the basic type variable of integer type (byte, short, int, long) is 0.
     e. The default value of the basic type variable of single-precision floating point type (float) is 0.0f.
     f. The default value of the basic type variable of double-precision floating-point type (double) is 0.0d.
     g. The default value of the basic type variable of character type (char) is "/u0000".
 *
 * 2: Inheritance
 * a: The classes in the package can only inherit the classes in the package, and cannot inherit the classes that are not in the package; I don't know why
 * b: Classes not in the package have the right to inherit all classes
 *
 *
 * */
class CloneClass implements Cloneable{
     int var1;int var2;int var3;
      protected Object clone() throws CloneNotSupportedException {
         return super.clone();
     }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325447111&siteId=291194637