Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String question

 
Fes D Gaur
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 335
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 220
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Santana Iyer
Ranch Hand
Posts: 335
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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

 
Akhilesh Trivedi
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic