What is the difference between “text” and new String(“text”)?

后端 未结 12 1146
悲&欢浪女
悲&欢浪女 2020-11-21 04:42

What is the difference between these two following statements?

String s = \"text\";

String s = new String(\"text\");
12条回答
  •  后悔当初
    2020-11-21 05:05

    Sorry for late Answer but much needed Answer. First we need to know some Java.lang.String Class rules.

    1. String Literals e.g.String str="java"; (we use only double Quotes) are different from String Object (we use new keyword) e.g. String str=new String("java");

    2. String is Immutable Object i.e. If value changes a new Object is created and returned to you eg See replace() and replaceAll() functions and many more.

    3. This creates a problem of many String Object in Modification, So creators of Java came up an Idea was called StringPool. StringPool is stored in heap area where object reference data will be stored as we know String is Char[](before java 9 very Long to read) or byte[](after java 9 short to read).

    4. String literals are stored in StringPool and String Objects are stored in as usual heap Object Area.

    5. If there are many Object String Initialization JVM heap will be finished in String Operations only, Java Development team came up with intern() solution this moves/changes memory reference to StringPool.

      Program: Comparing String references to objects

    Another good link to understand java.lang.String better

    import java.util.*; 
    
    class GFG { 
        public static void main(String[] args) 
        { 
          String siteName1 = "java.com";
            String siteName2 = "java.com";
            String siteName3 = new String("java.com");
            String siteName4 = new String("java.com").intern();
          
        System.out.println("siteName1:::"+Integer.toHexString(System.identityHashCode(siteName1)));
          System.out.println("siteName2:::"+Integer.toHexString(System.identityHashCode(siteName2)));
          System.out.println("siteName3 creation Of New Object Without Interned:::"+Integer.toHexString(System.identityHashCode(siteName3)));//must be Diffrent bcoz new Object In Heap Area
          System.out.println("siteName4 creation Of New Object With Interned:::"+Integer.toHexString(System.identityHashCode(siteName4)));//must be same MemoryAddress of siteName1,siteName2 and Interned, bcoz Objects Points to String pool Now
          
          System.out.println(siteName1 == siteName2); // true
          System.out.println(siteName1 == siteName3); // false this tells about lietral vs String Objects
          String siteName5 = siteName3.intern(); // Interning will not change Original Object but gives us a new Object
          
          System.out.println("siteName5 Interned from siteName3:::"+Integer.toHexString(System.identityHashCode(siteName5)));//must be same MemoryAddress of siteName1,siteName2 and Interned, bcoz Objects Points to String pool Now
          
          System.out.println(siteName1 == siteName3); // false this tells about Immutability
          System.out.println(siteName1 == siteName5); // true After Intering both are same
          System.out.println(siteName1 == siteName4); // true
          System.out.println(siteName5 == siteName4); // true
        } 
    }
    

提交回复
热议问题