Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2006

Tip: Looking for answers? Try searching our database.

Could some1 please explain this String behaviour

Thread view: 
priyom - 24 Nov 2006 15:15 GMT
Hi,

if(" String ".trim() == "String")

           System.out.println("Equal");

       else

           System.out.println("Not Equal");

this prints - not equal
whereas

       if("String".toString() == "String")

           System.out.println("Equal");

       else

           System.out.println("Not Equal");

prints Equal!
Shouldn't " String ".trim() return "String" from the constant string
pool ...and thus shouldnt "Equal" be printed?

Thanks in advance

Priyom
Patricia Shanahan - 24 Nov 2006 15:24 GMT
...
> Shouldn't " String ".trim() return "String" from the constant string
> pool ...and thus shouldnt "Equal" be printed?
...

See the JLS,
http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#5313
for the definition, for Java purposes, of "constant expression".

" String ".trim() is composed using an operation, method invocation,
that is not on the list, so it is not a constant expression of type String.

Patricia
Daniel Pitts - 24 Nov 2006 18:37 GMT
> Hi,
>
[quoted text clipped - 24 lines]
>
> Priyom
When comparing String values, you must use the .equals method

" String ".trim().equals("String") // This is true
" String ".trim() == "String" // This may or may not be true, depending
on a lot of things.

The problem here is the difference between reference equality and
object equality.

Reference equality basically says that reference's to the objects are
both the same value (They both point to the same place in virtual
memory, sort-of-speak).  Object equality says that the semantec meaning
of the object is the same, even if they are two different objects...

" String ".trim() will return a String object who's value is "String",
but there may be (and probably is) another "String" object somewhere
else.  Then your == is like asking "Is this apple the same apple as
that apple?"  They look the same, but they are indeed different
objects.

I hope this helps.

Daniel.
Simon Brooke - 24 Nov 2006 18:52 GMT
> Hi,
>
[quoted text clipped - 20 lines]
> Shouldn't " String ".trim() return "String" from the constant string
> pool ...and thus shouldnt "Equal" be printed?

This is the old pointer identity versus value identity bug.

If you used " String ".trim().equals( "String"), you'd find it would work.
The == operator tests pointer identity (for anything except primitives)
and so fails if you have two different Strings both with the print
value "String".

Signature

simon@jasmine.org.uk (Simon Brooke) http://www.jasmine.org.uk/~simon/
       "The result is a language that... not even its mother could
       love.  Like the camel, Common Lisp is a horse designed by
       committee. Camels do have their uses."
                                   ;; Scott Fahlman,  7 March 1995

Oliver Wong - 24 Nov 2006 19:27 GMT
> Hi,
>
[quoted text clipped - 20 lines]
> Shouldn't " String ".trim() return "String" from the constant string
> pool ...and thus shouldnt "Equal" be printed?

   Here's what I suspect is going on:

   In the first program, 4 strings are added to the constant pool: " String
", "String", "Equal" and "Not Equal". If you actually look at the source
code for the String class, you'll see that it holds not only a character
buffer, but also an offset and a length, so that two strings could share the
same buffer. So when you call trim() on " String ", what happens is that the
trim() method returns a new String object, which uses the same buffer, but
increases the offset by one, and decreases the length by two.

   When you compare the two strings using ==, the two strings are clearly
different objects, and so "Not Equal" is printed to the console.

   In the second program, 3 strings are added to the constant pool:
"String", "Equal" and "Not Equal". The same String object is used for both
instances of "String" which appear in the source code. When you call
toString() on "String", it just returns *this*, rather than creating a new
String object, and therefore you have two references to the same object,
which is why the == operator yields true, an "Equal" is returned.

   - Oliver
priyom - 25 Nov 2006 08:59 GMT
Hey thanks Oliver...that was of great help...

> > Hi,
> >
[quoted text clipped - 42 lines]
>
>     - Oliver


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.