String problem

G

gk

//--------- part -1--------------

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

System.out.println("Equal"); // prints

else

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



//--------- part -2--------------



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

System.out.println("Equal");

else

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








part-1 prints because there are 2 same string literals are
compared...hence it prints 'Equal'



Now, look at part-2.
It prints 'Not Equal'

Question is WHY it prints 'Not Equal' ?


first let us look at,
" String ".trim()

according to API , trim() Returns a copy of the string, with leading
and trailing whitespace omitted.

so ultimately it becomes " String ".trim() = "String" ...is not it ?

IF , its so then it is as similar as part-1 and hence the output should
have been 'Equal' BUT output is 'Not Equal'


WHY ?
 
R

RedGrittyBrick

gk said:
//--------- part -1--------------

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

System.out.println("Equal"); // prints

else

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



//--------- part -2--------------



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

System.out.println("Equal");

else

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








part-1 prints because there are 2 same string literals are
compared...hence it prints 'Equal'



Now, look at part-2.
It prints 'Not Equal'

Question is WHY it prints 'Not Equal' ?


first let us look at,
" String ".trim()

according to API , trim() Returns a copy of the string, with leading
and trailing whitespace omitted.

so ultimately it becomes " String ".trim() = "String" ...is not it ?

IF , its so then it is as similar as part-1 and hence the output should
have been 'Equal' BUT output is 'Not Equal'


WHY ?

This is a FAQ.

Use .equals() to compare String *values*. The "==" operator tests if
they are the same *object*.

Two *different* objects may contain the same value.

in your case I imagine that the JVM decided to create a new String
object for the result of the trim() operation whereas it was trivially
able to assign the literal values to a common object.
 
L

Lew

This is a FAQ.

Use .equals() to compare String *values*. The "==" operator tests if
they are the same *object*.

Two *different* objects may contain the same value.

in your case I imagine that the JVM decided to create a new String
object for the result of the trim() operation whereas it was trivially
able to assign the literal values to a common object.

Not only "trivially able to assign the literal values to a common object" but
required to do so (JLS 3.10.5):
http://java.sun.com/docs/books/jls/third_edition/html/lexical.html#101083

- Lew
 
G

gk

RedGrittyBrick said:
This is a FAQ.

Use .equals() to compare String *values*. The "==" operator tests if
they are the same *object*.

Two *different* objects may contain the same value.

in your case I imagine that the JVM decided to create a new String
object for the result of the trim() operation whereas it was trivially
able to assign the literal values to a common object.

do you mean a new String object is created because of trim() ?

see the API "trim() Returns a copy of the string, with leading
and trailing whitespace omitted."

does copy means here a new String object ?
 
P

Patricia Shanahan

gk wrote:
....
do you mean a new String object is created because of trim() ?

see the API "trim() Returns a copy of the string, with leading
and trailing whitespace omitted."

does copy means here a new String object ?

Yes. Note that it does not say anything like "Returns the original
string if it has no leading and trailing spaces, or a copy with leading
and trailing whitespace omitted if there is any."

Patricia
 
S

Simon Brooke

gk said:
//--------- part -1--------------

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

System.out.println("Equal"); // prints

else

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



//--------- part -2--------------



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

System.out.println("Equal");

else

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

This is the old LISP eq problem.

== means not 'is equivalent to', but 'is identically the same object as'.
That your first example worked is not specified by the language design but
is implementation dependent; it happened to work on your JVM. To test
whether two strings have the same sequence of characters, use

"String".equals( "String")


--
(e-mail address removed) (Simon Brooke) http://www.jasmine.org.uk/~simon/
;; This email may contain confidential or otherwise privileged
;; information, though, quite frankly, if you're not the intended
;; recipient and you've got nothing better to do than read other
;; folks' emails then I'm glad to have brightened up your sad little
;; life a tiny bit.
 
C

Chris Uppal

== means not 'is equivalent to', but 'is identically the same object as'.
That your first example worked is not specified by the language design but
is implementation dependent; it happened to work on your JVM.

At the risk of seeming pedantic...

Actually, the result of the example is defined (at least if you accept the
JavaDoc for java.lang.String.toString() as normative).

Since String.toString() is required to answer the string itself (not a copy);
and given the rules about duplicate occurences of string literals (they must
always refer to the same object); the result of
"String".toString() == "String"
/must/ be true in any conforming implementation.

-- chris
 
J

Jussi Piitulainen

Chris said:
At the risk of seeming pedantic...

Actually, the result of the example is defined (at least if you
accept the JavaDoc for java.lang.String.toString() as normative).

Since String.toString() is required to answer the string itself (not
a copy); and given the rules about duplicate occurences of string
literals (they must always refer to the same object); the result of
"String".toString() == "String"
/must/ be true in any conforming implementation.

The rule about String literals being the same is also given in the
String Javadoc. It's at .intern().
 

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,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top