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
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.
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
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.
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