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 question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String question" Watch "String question" New topic
Author

String question

Fes D Gaur
Ranch Hand

Joined: Apr 29, 2005
Posts: 54
if(" String ".trim() == "String")
System.out.println("Equal");
else
System.out.println("Not Equal");

The output is Not Equal.
When the compiler compiles this code does it not first trim the spaces from " String " and make a new String object "String" which is equal to "String". Or does the compiler perform the "==" test first and then perform the trim() ? Some help would be appreciated.

Thanks,
Fes
Santana Iyer
Ranch Hand

Joined: Jun 13, 2005
Posts: 335
you gave the answer it creates a new string

" hello".trim()=="hello"

see it creates a new string and so == returns false as == can only be true if both refer to same object.

(" hello".trim()).equals("hello"); now this is true as equals() checks contents.
Fes D Gaur
Ranch Hand

Joined: Apr 29, 2005
Posts: 54
But I thought String objects are pooled by the JVM. So:

" String ".trim() creates "String"

then == "String" wants to create another String object but sees that a "String" object already exists in the pool and does not create a new object. So both refer to the same "String" object. Is the order of execution the issue here? Meaning does the comparison happen before trim()?
Fes D Gaur
Ranch Hand

Joined: Apr 29, 2005
Posts: 54
Here are some more String comparisons that make me believe that the comparision "==" occurs before the String methods (trim(), toUpperCase()) are applied:

if("String".trim() == "String".trim())
System.out.println("Equal");
else
System.out.println("Not Equal");

output is "Equal" if "String".trim() is creating a new String then it should create two new Strings and "==" should come back with false. But here it comes back with true.

if( "STRING".toUpperCase() == "STRING")
System.out.println("Equal");
else
System.out.println("Not Equal");

output is "Equal". If "STRING".toUpperCase() creates a new String then the "==" should return false here. Even if a new String is created the comparison has to occur first for the result to come back as true.

Please let me know if something else is going on here.

Thanks,
Fes
Akshay Kiran
Ranch Hand

Joined: Aug 18, 2005
Posts: 220
I think this ought to answer all your doubts
from the API docs,
toUpperCase()
Returns:
the String, converted to uppercase.

trim()
Returns:
A copy of this string with leading and trailing white space removed, or this string if it has no leading or trailing white space.

I think that should put an end to your problems


"It's not enough that we do our best; sometimes we have to do<br />what's required."<br /> <br />-- Sir Winston Churchill
Santana Iyer
Ranch Hand

Joined: Jun 13, 2005
Posts: 335
if("String".trim() == "String".trim())
System.out.println("Equal");
else
System.out.println("Not Equal");

because if object is not modified than returns same reference as original string
bhavesh bhanushali
Ranch Hand

Joined: Jun 13, 2005
Posts: 55
public class StringCompare
{
public static void main ( String args[] )
{
if ( " String ".trim() == "String")
System.out.println ( " Yes they are equal " ) ;
else
System.out.println ( " No they are not equal " );

if ( " String ".trim() .equals ( "String".trim() ) )
System.out.println ( " Yes they are equal " ) ;
else
System.out.println ( " No they are not equal " );

}
}
2 observations
1] " == " operator compares the references ( i.e. the addresses the strings occupy in the buffer )
2] ".equal" operator compares the contents of the address locations i.e. the contents of the strings in this case

when you run the following code the difference between the 2 will hopefully
the difference will be clear

regards ,
Bhavesh
Sid Remey
Ranch Hand

Joined: Oct 02, 2005
Posts: 31
Hi,

Here are two more samples that should help you to understand strings and the string pool.

Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1527
Hi Fes,

Originally posted by Fes D Gaur:
But I thought String objects are pooled by the JVM. So:

" String ".trim() creates "String"

then == "String" wants to create another String object but sees that a "String" object already exists in the pool and does not create a new object. So both refer to the same "String" object. Is the order of execution the issue here? Meaning does the comparison happen before trim()?


Even I read that String Objects are pooled by JVM. But is it the case always??? or is it like Garbage Collection which you dont know when shall it happen??

Coming to evaluation of the following expression...

"String".trim() == "String".trim()


Theoretically it should be like left-hand-side trim() evaluated first, then the right-hand-side and then the comparison. what say?
[ October 03, 2005: Message edited by: Akhil Trivedi ]

Keep Smiling Always — My life is smoother when running silent. -paul
[FAQs] [Certification Guides] [The Linux Documentation Project]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: String question