0

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?

Moshiur Rahman
  • 347
  • 2
  • 8

1 Answers1

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