Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

trim() function regardings...

 
Ramesh Balakrishnan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • 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.
 
Ben Buchli
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • 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
 
Elouise Kivineva
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic