Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2007

Tip: Looking for answers? Try searching our database.

strange behavior of println()

Thread view: 
newvb - 13 Sep 2007 14:50 GMT
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
newvb - 13 Sep 2007 14:57 GMT
> 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 "==".
Thomas Fritsch - 13 Sep 2007 15:06 GMT
> 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

Roedy Green - 13 Sep 2007 19:44 GMT
>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

Roedy Green - 13 Sep 2007 20:18 GMT
>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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.