Could some1 please explain this String behaviour

P

priyom

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
 
P

Patricia Shanahan

D

Daniel Pitts

priyom said:
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
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.
 
S

Simon Brooke

priyom said:
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?

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".

--
(e-mail address removed) (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
 
O

Oliver Wong

priyom said:
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?

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
 
P

priyom

Hey thanks Oliver...that was of great help...

Oliver said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top