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

后端 未结 5 1301
不知归路
不知归路 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:50

    1. String t = new String("abc"); statement 1 will create an object on Heap, and additionally places an string literal in the pool having the same value.

    The reference variable t will refer to the object on the heap.

    1. String t = "abc";

    However statement 2 will only create an object in string constant pool if the object having same value is not present in the pool and t will refer the object placed in the string constant pool.

提交回复
热议问题