aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Help please Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Help please" Watch "Help please" New topic
Author

Help please

Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi ...
I thought I understood the difference between using the == operator and the equals() method with String objects but the following code has me thoroughly confused

I thought I would see:

Would someone please explain why they all came up true?
Thanks


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Paul A
Ranch Hand

Joined: Aug 25, 2000
Posts: 44
Pls see my post here... http://www.javaranch.com/ubb/Forum24/HTML/004044.html
------------------
http://pages.about.com/jqplus
Get Certified, Guaranteed!


<A HREF="http://pages.about.com/jqplus" TARGET=_blank rel="nofollow">http://pages.about.com/jqplus</A> <BR>Get Certified, Guaranteed!
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Thanks Paul. Think I've finally got it but just in case, does the following sound right:

  • if String objects having the same data are created using a constant expression, a string literal, a reference to an existing string, or by explicitly using the intern() method, their references will be the same
  • if String objects having the same data are created explicitly with the new operator or their values are computed at runtime, their references will be different


  • Thanks

    [This message has been edited by Jane Griscti (edited September 14, 2000).]
Barbara Dyer-Bennet
Greenhorn

Joined: Sep 14, 2000
Posts: 10
The way I read the line:
System.out.println("\t Equals: s1 == s4 \t\t " + (s1 != s4));
the expression (s1 != s4) comes up as true because you have a trick in there... s1 is NOT equal to s4, so the print statement displays the word true. I hope this helps.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
By the way, let me just add another excellent example which will clear everybody's concept about String literals:
Here it goes:

produces the output:
true true true true false true
This example illustrates six points:
Literal strings within the same class (�8) in the same package (�7) represent references to the same String object (�4.3.1).
Literal strings within different classes in the same package represent references to the same String object.
Literal strings within different classes in different packages likewise represent references to the same String object.
Strings computed by constant expressions (�15.28) are computed at compile time and then treated as if they were literals.
Strings computed at run time are newly created and therefore distinct.
The result of explicitly interning a computed string is the same string as any pre-existing literal string with the same contents.
Hope this helps all of us for the exam
-sampaths77
[I added UBB CODE tags to your source code to make it more readable. Please try to use them in the future. Learn more about UBB codes - Ajith]
[This message has been edited by Ajith Kallambella (edited September 15, 2000).]
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Thanks Barbara, Sampaths77 ... I noticed the "!=" after banging my head against the code for an hour or so Oh well, it forced me to go through the JLS on String literals so in the end I learned from it.
Jane
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help please