This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Check String SDK: Returns a canonical representation for the string object. A pool of strings, initially empty, is maintained privately by the class String.
When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.
It follows that for any two strings s and t, s.intern() == t.intern() is true if and only if s.equals(t) is true.
All literal strings and string-valued constant expressions are interned.
When a String literal is used, that String object is placed into a pool. If another reference is pointed to the same String literal, then instead of creating a new object, that reference points to the String literal in the pool.
When you call intern() on a String, it searches the String pool. If the String that you call intern() on appears as a String literal in the pool, then the intern method will return a reference to that String literal.
That is why you get true when you call intern() on the String object that is created.
Because "Test" is in the pool already, and the parameter refers to it.