Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

trim() function regardings...  RSS feed

 
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!