What is the difference between `String.intern()` and a regular String literal?

Java Associate Hard

Java Associate — Hard

What is the difference between `String.intern()` and a regular String literal?

Key points

  • `String.intern()` stores the String in the pool; literals are automatically interned
  • `String.intern()` returns the canonical reference
  • Objects created with new are not automatically interned
  • Regular literals are stored as-is

Ready to go further?

Related questions