> Here i wrote small code to test the different between equals and "=="
> public class TestString {
[quoted text clipped - 25 lines]
>
> Thanks
It turned out that I should write like this : "s == s1" + (s1.intern()
== s.intern()).
However, i still don't know why I must put s1.intern() == s.intern()
into bracket. The order of operator still imply this, doesn't it?
rossum - 13 Sep 2007 15:13 GMT
>It turned out that I should write like this : "s == s1" + (s1.intern()
>== s.intern()).
>
>However, i still don't know why I must put s1.intern() == s.intern()
>into bracket. The order of operator still imply this, doesn't it?
The + operator has a higher precedence than the == operator so:
A + B == C
will be read as:
(A + B) == C
which is what seems to have happened in your case.
rossum
Thomas Fritsch - 13 Sep 2007 15:36 GMT
> It turned out that I should write like this : "s == s1" + (s1.intern()
> == s.intern()).
>
> However, i still don't know why I must put s1.intern() == s.intern()
> into bracket. The order of operator still imply this, doesn't it?
See http://java.sun.com/docs/books/tutorial/java/nutsandbolts/operators.html
My recommendation:
Don't try to memorize the whole operator precedence table. It is too
difficult anyway.
Instead form the habit of using redundant ( ) in any case where you are
in doubt or where your co-workers might be in doubt.

Signature
Thomas
david.karr - 13 Sep 2007 18:04 GMT
> > Here i wrote small code to test the different between equals and "=="
> > public class TestString {
[quoted text clipped - 31 lines]
> However, i still don't know why I must put s1.intern() == s.intern()
> into bracket. The order of operator still imply this, doesn't it?
Nope. "+" binds tighter than "==".
> Here i wrote small code to test the different between equals and "=="
> public class TestString {
[quoted text clipped - 14 lines]
> For the last output s==s1 is missed. Does anyone have some idea about
> this?
Your line
System.out.println("s == s1: " + s1.intern()==s.intern());
is processed left to right, because you missed some parentheses.
Therefore it is effectively processed like
System.out.println(("s == s1: " + s1.intern()) == s.intern());
What you want, has to be parenthesized like this
System.out.println("s == s1: " + (s1.intern()) == s.intern()));
It will print
s == s1: true

Signature
Thomas
>For the last output s==s1 is missed. Does anyone have some idea about
>this?
see http://mindprod.com/jgloss/string.html#COMPARISON
http://mindprod.com/jgloss/intern.html
It think it odd you stumbled on the relatively advanced intern without
first encountering the core concept -- the difference between == and
equals.

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
>System.out.println("s == s1: " + s1.intern()==s.intern());
for why you need the extra parentheses, see
http://mindprod.com/jgloss/precedence.html
+ is evaluated before ==

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com