0
String s = new String("Java");

Will this statement creates two string objects. One which is stored in heap and another in string pool.

I have searched a lot but couldn't find any documentation.

If yes/no please give me reason/reference for the same.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
Babu Reddy H
  • 186
  • 1
  • 11

1 Answers1

-1

Lets talk some cases related to this.

String s1 = "java";
String s2 = "java";
String s3 = new String("java");

a) s1 and s2 are just referenced,not objects, and they are pointing to the same String in memory.

b) The "java" is interned and is a compound object: one char[] object and the String object itself. It consisting of two objects in memory.

c) s3, new String("java") produces one more object. The new String("java") does not copy the char[] of "java", it only references it internally. Here is the method signature:

public String2(String original) {
        this.value = original.value;
        this.hash = original.hash;
}
RockAndRoll
  • 2,247
  • 2
  • 16
  • 35