Java automatically interns String literals. This means that in many cases, the == operator appears to work for Strings in the same way that it does for ints or other primitive values.
Since interning is automatic for String literals, the intern() method is to be used on Strings constructed with new String()
Using your example:
When two strings are created independently, intern() allows you to compare them and also it helps you in creating a reference in the string pool if the reference didn't exist before.
When you use String s = new String(hi), java creates a new instance of the string, but when you use String s = "hi", java checks if there is an instance of word "hi" in the code or not and if it exists, it just returns the reference.
Since comparing strings is based on reference, intern() helps in you creating a reference and allows you to compare the contents of the strings.
When you use intern() in the code, it clears of the space used by the string referring to the same object and just returns the reference of the already existing same object in memory.