> //--------- part -1--------------
>
[quoted text clipped - 36 lines]
>
> 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.
Lew - 01 Oct 2006 21:05 GMT
>> //--------- part -1--------------
>> if("String".toString() == "String")
[quoted text clipped - 15 lines]
>>
>> Question is WHY it prints 'Not Equal' ?
> This is a FAQ.
>
[quoted text clipped - 6 lines]
> 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
gk - 02 Oct 2006 06:39 GMT
> > //--------- part -1--------------
> >
[quoted text clipped - 47 lines]
> 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 ?
Patricia Shanahan - 02 Oct 2006 08:53 GMT
...
> 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
> //--------- part -1--------------
>
[quoted text clipped - 15 lines]
>
> 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")

Signature
simon@jasmine.org.uk (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.
Chris Uppal - 02 Oct 2006 10:45 GMT
> > if("String".toString() == "String")
> == 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
Jussi Piitulainen - 02 Oct 2006 11:54 GMT
>>> if("String".toString() == "String")
>
[quoted text clipped - 13 lines]
> "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().
Lew - 03 Oct 2006 06:59 GMT
>> Actually, the result of the example is defined (at least if you
>> accept the JavaDoc for java.lang.String.toString() as normative).
> The rule about String literals being the same is also given in the
> String Javadoc. It's at .intern().
And in the Java Language Specificiation!
(JLS 3.10.5):
http://java.sun.com/docs/books/jls/third_edition/html/lexical.html#101083
It is not just a good idea, it's the law.
- Lew