jQuery in Action, 2nd edition*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes problem with equal and == Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "problem with equal and ==" Watch "problem with equal and ==" New topic
Author

problem with equal and ==

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Aug 26, 2001
Posts: 7610
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).]


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Aug 30, 2000
Posts: 3141
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


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Thanks everybody. It really helps. I'm very grateful.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: problem with equal and ==