What is the difference between
String str = new String("abc");
and
String str = "abc";
ie what is the difference between String Literal and String object.
Ans:
In this example both string literals refer the same object:
String a = "abc";
String b = "abc";
System.out.println(a == b); // true
Here, 2 different objects are created and they have different references:
String c = new String("abc");
String d = new String("abc");
System.out.println(c == d); // false
String Literal uses constant pool memory whereas String Object refers the Heap memory.
When we create string b it doesnot create another string but but use the reference of the a String.
Comments
Post a Comment