File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String Confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String Confusion" Watch "String Confusion" New topic
Author

String Confusion

Fes D Gaur
Ranch Hand

Joined: Apr 29, 2005
Posts: 54
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

Joined: Oct 02, 2003
Posts: 11498
    
  16

have you tried running it to see what the output is?

What is it outputting that is different from what you'd expect?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Fes D Gaur
Ranch Hand

Joined: Apr 29, 2005
Posts: 54
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

Joined: May 22, 2003
Posts: 657

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

Joined: Oct 02, 2003
Posts: 11498
    
  16

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

Joined: Apr 29, 2005
Posts: 54
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

Joined: Sep 28, 2005
Posts: 225
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.


Regards,<br />Seb<br /> <br />SCJP 1.4
 
jQuery in Action, 2nd edition
 
subject: String Confusion