• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

String

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String s1 = new String ("abcd");
String s2 = "abcd";
how come s1== s2 returns false, doesn't s1 and s2 point to the same object?
when does the complier create a new String object ?
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all, the compiler never creates new objects - it only compiles code. It's up to the JVM to do things like that.
The key to this question is the fact that String literals are handled a little differently than when you create a String object with the new keyword.
String literals are created when the class is loaded and a reference to them is made from a constant table. String objects created with the word new are created when that line is executed.
Even though the two Strings contain the same value, they are not the same object. That's what the == operator checks when used on two objects, to see if they are really the same object. In this case, they are not. If you'd like to compare the contents of the two Strings, use the equals() method defined within String.
I hope that helps,
Corey
 
Maria Garcia
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what difference does "new" make in this example ?
i thought that once a String object is made, JVM looks for that same object and when another variable tries to reference it, it just makes the latter point to it therefore using the previously created object.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Maria Garcia:
what difference does "new" make in this example ?
i thought that once a String object is made, JVM looks for that same object and when another variable tries to reference it, it just makes the latter point to it therefore using the previously created object.


That behavior only occurs when using String literals. Try this:

and compare the output to this:

You'll find that the first one prints true while the second one prints false.
The difference is that the String referred to by the literal "test" is created when the class is loaded. Whenever you use that literal in your code, you get a reference to that String object.
However, when you use the keyword new, a brand new String object is created, even if a String with the same contents exists within the String literal pool.
You might want to check out Cindy's article in this last month's newsletter, Life in the Constant Pool. It discusses this behavior.
I hope that helps,
Corey
 
Maria Garcia
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks corey
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay I understand why s1 == s2 returns false. But why does (s1.equals(s2)) return true??? Is s1 a Sting object? Is a String literal an object.
Also if
String s3 = "abcd"
then
(s1 == s3) returns true and
(s1.equals(s2)) returns true?
I know this to be true but cannot explain it.
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Also if
String s3 = "abcd"
then
(s1 == s3) returns true and
(s1.equals(s2)) returns true?



' == ' Operator determines whether the variables refrence the same object in memory and returns true only if references are to same object.
if you construct two Strings with same literal without using new keyword ie
String a = "hello";
String b = "hello";
if(a == b) // this will return true.
String and Wrapper classes override Object class's equals() method.
' equals() ' method compares the contents of the objects and checks for content equality.
eg: String s3 = new String("abcd");
String s4 = new String("abcd");
System.out.println(s3.equals(s4)); // true
System.out.println((s3==s4)); //false
Long l = new Long(100);
Long lol = new Long (100);
System.out.println(l.equals(lol));

Note: StringBuffer do not override equals() method whereas String class does.
Hope this helps
-Bani
 
If you open the box, you will find Heisenberg strangling Shrodenger's cat. And waving this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic