79176136

Date: 2024-11-11 01:12:59
Score: 1
Natty:
Report link

I understand your confusion. That's because every peace of code in your class which is enclosed with either " or """ (called string literal and text block respectfully) are beeing added into String Pool during compile time. Whereas the keyword "new" doesn't add anything into String Pool instead it creates a reference to an object of type String and puts it somewhere into the memory heap during runtime. String literal "Java" and variable s1 point to different objects in heap memory so that's why you have to use String.intern when comparing them during runtime.

The only time when String.intern adds new string into the String Pool is when it doesn't already contain that exact string.

Source: java specification ยง3.10.5.

Reasons:
  • Long answer (-0.5):
  • No code block (0.5):
  • Low reputation (1):
Posted by: jacksolovey