What is the difference between strings allocated using new operator & without new operator in java J2ME?

后端 未结 5 1295
不知归路
不知归路 2020-12-03 23:39

what is the Difference between

String str=new String(\"Thamilan\");

and

String str=\"Thamilan\";

in java

5条回答
  •  再見小時候
    2020-12-03 23:53

    An answer from Java String declaration

       String str = new String("SOME") 
    

    always create a new object on the heap

        String str="SOME"  
    

    uses the String pool

    Try this small example:

        String s1 = new String("hello");         
        String s2 = "hello";
         String s3 = "hello";
          System.err.println(s1 == s2);
         System.err.println(s2 == s3); 
    

    To avoid creating unnecesary objects on the heap use the second form.

提交回复
热议问题