• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

trim() function regardings...

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello people,
I have a doubt in trim() function. I executed the following codelet.
String s="ram".trim();
String k="ram ".trim();
if(s==k)
System.out.println("yes");
else
System.out.println("no");
I got "no" output..
if I use equals() function in if checking then I get "yes" output.Please let me know the things happen here.
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
always use string1.equalsTo( string2 ) or string1.equalsIgnoreCase( string2 ) when comparing Strings.
I'm sure it will work if you try it like that.
Ben
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://mindprod.com/jgloss/gotchas.html#COMPARISON
 
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The important thing here is that when you compare two objects (NOTE: Strings are objects, not primitives) using == you are asking whether these two objects are located at the exact same space in the computer's memory, i.e. are the physically one and the same object. You are NOT asking if they are two different objects of logically equal value.
reply
    Bookmark Topic Watch Topic
  • New Topic