strange behavior of println()

N

newvb

Here i wrote small code to test the different between equals and "=="
public class TestString {
public static void main(String[] args){
String s = "Hello World";
String s1 = new String("Hello World");
String s2 =s.intern();
String s3 = "Hello World";

System.out.println("s == s1: " + (s==s1));
System.out.println("s == s2: " + (s==s2));
System.out.println("s == s3: " + (s==s3));


System.out.println("s equal s1: " + s.equals(s1));
System.out.println("s == s1: " + s1.intern()==s.intern());

}
}

However, I got some strange output as follows:
s == s1: false
s == s2: true
s == s3: true
false

For the last output s==s1 is missed. Does anyone have some idea about
this?

Thanks
 
N

newvb

Here i wrote small code to test the different between equals and "=="
public class TestString {
public static void main(String[] args){
String s = "Hello World";
String s1 = new String("Hello World");
String s2 =s.intern();
String s3 = "Hello World";

System.out.println("s == s1: " + (s==s1));
System.out.println("s == s2: " + (s==s2));
System.out.println("s == s3: " + (s==s3));

System.out.println("s equal s1: " + s.equals(s1));
System.out.println("s == s1: " + s1.intern()==s.intern());

}

}

However, I got some strange output as follows:
s == s1: false
s == s2: true
s == s3: true
false

For the last output s==s1 is missed. Does anyone have some idea about
this?

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?
 
T

Thomas Fritsch

newvb said:
Here i wrote small code to test the different between equals and "=="
public class TestString {
String s = "Hello World";
String s1 = new String("Hello World");
System.out.println("s equal s1: " + s.equals(s1));
System.out.println("s == s1: " + s1.intern()==s.intern());

}
}

However, I got some strange output as follows:
s == s1: false
s == s2: true
s == s3: true
false

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
 
R

rossum

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
 
T

Thomas Fritsch

newvb said:
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.
 
D

david.karr

Here i wrote small code to test the different between equals and "=="
public class TestString {
public static void main(String[] args){
String s = "Hello World";
String s1 = new String("Hello World");
String s2 =s.intern();
String s3 = "Hello World";
System.out.println("s == s1: " + (s==s1));
System.out.println("s == s2: " + (s==s2));
System.out.println("s == s3: " + (s==s3));
System.out.println("s equal s1: " + s.equals(s1));
System.out.println("s == s1: " + s1.intern()==s.intern());


However, I got some strange output as follows:
s == s1: false
s == s2: true
s == s3: true
false
For the last output s==s1 is missed. Does anyone have some idea about
this?

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?

Nope. "+" binds tighter than "==".
 

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,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top