• 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

String

 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if(" hello ".trim()=="hello")
System.out.println("true");
else
System.out.println("false");


in above code am getting o/p is false.

please explain this
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
while comparing two strings, you need to use 'equals' or 'equalsIgnoreCase' instead of '=='.

Regards,
KJ
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kavitha,
KJ is right. Whenever you want to compare the contents use equals() method.
When you use == operator, you are comparing objects, not the contents.
In you example "hello".trim() and "hello" are two different objects, so the output is false.

Thanks,
Pradnya.
 
kavitha vasu
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi pradnya


"hello ".trim()=="hello"


here am comparing two string literals not String objects



for example

if("hello".replace('l','L')=="hello")

above code returns false

leftside literal changed as heLLo

example 2

if("hello".replace('l','l')=="hello")

returns true


but why am getting

"hello ".trim()=="hello" as false



so confusing
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kavitha,


quote:


-----------------------------------------------------------------

"hello".replace('l','l')=="hello" returns true

-----------------------------------------------------------------

"hello".replace('l','l')=="hello" never returns true.Please verify it once again and revert.

It is always new String Object whenever you use some method on a String literal.

Here you are using replace() method on a String literal "hello" so there is a new String Object in the String Object pool.
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Yes, every method called on String object creates a new String object and is returned. But this happens only when the returned string is not exactly same as the one on which it is called.
That means, in "hello".replace('l','l') since the returned String is not modified, it just returns the same string object which it was previously. So "hello".replace('l','l')=="hello" returns true.
But "hello ".trim() returns a new String object. So "hello ".trim() =="hello" returns false.
 
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by kesava narayana:
Yes, every method called on String object creates a new String object and is returned. But this happens only when the returned string is not exactly same as the one on which it is called.



Do note that this bolded statement is technically no longer guaranteed as of Java 5. This was true for older versions of the Java API, but it's now no longer guaranteed that replace(), toUpperCase(), etc will return the original string if there was no change. (But, strangely, this guarantee is still there for trim().) While it's true that most common implementations do still seem to behave this way, you should not rely on this behavior in your programs!
 
Kishore Kumar
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Kelvin,I am using java 1.5 version. But still toLowerCase(),toUpperCase() still returning the same String object if the string is not modified upon calling these methods. Is it in version 1.6 what you said.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic