Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

String Trim

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

I believe that the following piece of code should give an answer true. But when complied, it gives false. Can someone please help me with this?



Regards
Vijay
 
Ranch Hand
Posts: 119
Spring MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Vijay,

I think it becuase when you are comparing a string using == operator, using this operator Java will compare the reference address of those strings. If you want to compare their contents use String.equals() method instead.

Regards,
Wayan
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

[ October 17, 2005: Message edited by: Ken Blair ]
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ken Blair:


[ October 17, 2005: Message edited by: Ken Blair ]



I agree that your last example is bad, but it is entirely predictable if you understand how == works with String objects. In this case, the condition of the if statement evaluates to true because you are using String literals here. If you want to know more, you should google for "String Constant Pool" or something along those lines.

Layne
 
Vijay Jagannathan
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your comments. Sorry for not being more specific.

But what I was confused with was this:
-> When a new String is created and exists in the pool, both the new and existing String will point to the same object.

Based on this idea, when a String Object "String" is created and a second string object "String" is compared to it, it should point to the same object and the code should return a true.

For Example:



will return true.

But my code above



returns false. Does this have anything to do with the trim() function?

Regards
Vijay
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The first thing to do is to check the API for String - do not trust your intuition or "feeling". You will read in the API for the trim method that " XXXXXXX ".trim() returns a new String object. Although the String object will contain the characters XXXXXXX, it does not say that the new String object will be the same as one previously defined as a String literal as in "XXXXXXX" or as a previously interned String containing those characters. So a "==" comparison will evaluate to false.

You may use the intern method of the String class to enter a string into the pool:

 
reply
    Bookmark Topic Watch Topic
  • New Topic