Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem with equal and ==

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everybody,
This compile and print "true false true true"
Since the second println and the last one is expressing the same thing I thought this should have printed true "false true false"
please if any one have idea I will be very grateful.
public class Test
{
public static void main(String args[])
{
System.out.println( " String ".trim().equals("String") );
System.out.println( " String ".trim() == ("String") );
System.out.println( "String".trim().equals("String") );
System.out.println( "String".trim() == ("String") );
}
}
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ify,
In fact the last statement being

will not return false since no new String instance is created in result to the invocation of trim() on "String". "String".trim() will not create a new String since "String" is already present in the String pool of the class Test.
Note that in the second statement there is a space just after String ("String ") and then trim() will trim the space and thus return a new String, but no new String is created if the change does not change anything.
HIH
Val
[This message has been edited by Valentin Crettaz (edited September 20, 2001).]
[This message has been edited by Valentin Crettaz (edited September 20, 2001).]
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ifi!
1� System.out.println( " String ".trim().equals("String") );
" String ".trim() Create a new String "String" which is equal to "String" so -----> true
2� System.out.println( " String ".trim() == ("String") );
" String ".trim() Create a new String, the operator " == " compare references, so -----> false
3� System.out.println( "String".trim().equals("String") );
"String".trim() return the same "string" which is equal to "String" so -----> true
4� System.out.println( "String".trim() == ("String") );
return the same "string" which is equal to "String" so -----> true
The key concept is " String ".trim() ------> return a new String and "String".trim()--------> return the same String.
Hope thats hepl and corrcet if I�m wrong
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ify,
In the second println
<pre>
System.out.println( " String ".trim() == ("String") );
</pre>
The <code>trim()</code> operation removes the spaces and returns a new string object with the chars "String".
In the last println
<pre>
System.out.println( "String".trim() == ("String") );
</pre>
there is nothing for <code>trim()</code> to do! There are no empty spaces to remove so the original String object is returned.
If the original string object satisfies the contract of the method being called, a reference to the original string object is returned, a new object is not created.
This is one of the ways Java optimizes string handling.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everybody. It really helps. I'm very grateful.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic