Win a copy of Transfer Learning for Natural Language Processing (MEAP) this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

problem with equal and ==

 
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") );
}
}
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • 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
 
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.
 
Why is the word "abbreviation" so long? And this ad is so short?
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic