What is the result of this code? “`java String s1 = new String(“hello”); String s2 = s1.intern(); String s3 = “hello”; System.out.println(s1 == s2); System.out.println(s2 == s3); “`

Java Professional Hard

Java Professional — Hard

What is the result of this code? “`java String s1 = new String(“hello”); String s2 = s1.intern(); String s3 = “hello”; System.out.println(s1 == s2); System.out.println(s2 == s3); “`

Key points

  • The `intern()` method returns a canonical representation of the string
  • `==` compares references, not values
  • String literals are stored in a string pool
  • `s1` and `s2` are different objects, while `s2` and `s3` point to the same string literal

Ready to go further?

Related questions