String s1 = new String("string") creates two object in java.one in String pool and one in heap. now if i write another statement after this like String s2 = "string". will it create another object in String pool or returns the previous object's reference?
Asked
Active
Viewed 67 times
1 Answers
2
String s2 = "string";
will return object from string pool.
String s1 = new String("s");
String s2 = "s"; // from pool
String s3 = "s"; // from pool
System.out.println(s1 == s2); // false
System.out.println(s3 == s2); // true

Artem Petrov
- 772
- 4
- 17
-
that object which was created by calling new String("string") ? – Moshiur Rahman Jul 11 '17 at 14:36
-
"string" - is first object which would be put into string pool, new String("string") - is second object (not from string pool). s2 will be assigned to object from string pool. – Artem Petrov Jul 11 '17 at 14:38