Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String Confusion

 
Fes D Gaur
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With an understanding that "==" compares the value of references and equals compares the contents and with an understanding of trim() and toUpperCase() and other String methods, what is the output of the following code and why?

public class StringCompare
{
public static void main ( String args[] )
{
//#1
if ( " String ".trim() == " String ".trim())
System.out.println ( " Yes they are equal " ) ;
else
System.out.println ( " No they are not equal " );
//#2
if ( " String ".trim() == "String")
System.out.println ( " Yes they are equal " ) ;
else
System.out.println ( " No they are not equal " );
//#3
if ( "String".trim() == "String")
System.out.println ( " Yes they are equal " ) ;
else
System.out.println ( " No they are not equal " );
//#4
if ( "String".toUpperCase() == "STRING")
System.out.println ( " Yes they are equal " ) ;
else
System.out.println ( " No they are not equal " );
//#5
if ( "String".substring(0,2) == "String")
System.out.println ( " Yes they are equal " ) ;
else
System.out.println ( " No they are not equal " );

}
}

Thanks,

Fes
 
fred rosenberger
lowercase baba
Bartender
Posts: 12147
31
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
have you tried running it to see what the output is?

What is it outputting that is different from what you'd expect?
 
Fes D Gaur
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Output is:

No they are not equal
No they are not equal
Yes they are equal
No they are not equal
No they are not equal

The confusion is this:

#1 both Strings are " String " and according to String pooling both should be the same object. So their reference should be the same. Yet the output is"No they are not equal". Why?

#2 The result of " String ".trim() is "String". Why is the output "No they are not equal"?

#3 No confusion here

#4 The result of "String".toUpperCase() is "STRING". Why is the output "No they are not equal"?

#5 At first I thought that the String comparison is occuring before the methods are being applied. But #5 contradicts this.

So what is the clear cut rule for these comparisons?
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear in mind that the == operator compares reference values, not the contents of a String. Also bear in mind that trim() creates a new String object (strings are immutable).

Does that help?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12147
31
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
remember that string functions don't change the original string. they create new strings.

when new strings are created at runtime (like using trim()), they are NOT in the string pool.

so, when you do


your SOURCE strings are identical. but then you create a NEW string with the first trim. you then create ANOTHER NEW string with the second trim.

"==" compares to see if we are pointing to the same object (i.e. memory space). since we have two different strings (which contain the same data), we get a false for this first case.

Hopefully, this will help make sense of the other cases.
 
Fes D Gaur
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys,

That helps clear up the confusion. So if the resulting string is exactly the same as the original string then it's the same object. As is the case with:

"String".trim() == "String" //equal

But if the resulting string is different as is the case with:

" String ".trim() == "String" //not equal
 
Seb Mathe
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, for most (all ?) string operations, if the result is the same than the original String object, the result is the object itself, else another String is created.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic