Feature |
|
|
|---|---|---|
Memory Location | Stored in the String Constant Pool. | Stored directly in Heap memory, outside the pool. |
Object Creation | Reuses an existing object if the exact text is already in the pool. | Always forces Java to create a brand-new object in memory. |
Performance | Faster and saves memory by avoiding duplicate objects. | Slower and uses extra memory. |
| Two identical literals return | Returns |
String a = "Java";
String b = new String("Java");
System.out.println(a == b); # false
System.out.println(a.equals(b)); # true