..If a class doesn’t have an equals method, Java determines whether the references point to the same object – which is exactly what == does. In case you are wondering, the authors of StringBuilder did not implement equals(). If you call equals() on two StringBuilder instances, it will check reference equality.
The string pool contains literal values that appear in your program. For example, “name” is a literal and therefore goes into the string pool. myObject.toString() is a string but not a literal, so it does not go into the string pool. Strings not in the string pool are garbage collected just like any other object.
I'm not sure what authors intended to be there, but in both cases it is "true". Because t1 == t1 comparing the same reference variable with itself, so it is "true". t1 == t3 is also "true", because "Tiger t3 = t1;" (t1 object reference being assigned to t3 reference variable, so these two references refers to the same object.Maybe authors intended to write System.out.println(t1 == t3); on line 7.
Yes, you're correct. It is like this, because StringBuilder class is inherited from Object class, which is the superclass (or base class) of all classes in Java, and this inherited method compares references equality. So it gives you always "false" unless you supply the same reference variables as arguments. String class overwrite this "equals" method, so it gives different results.According this I understand that the result of == and equals() are always the same for two StringBuilder instances, unlike String. Am I correct or not?
Tim Driven Development | Test until the fear goes away
Mushfiq Mammadov wrote:Maybe authors intended to write System.out.println(t1 == t3); on line 7.
Mushfiq Mammadov wrote:According this I understand that the result of == and equals() are always the same for two StringBuilder instances, unlike String. Am I correct or not?
Mushfiq Mammadov wrote:s1 and s2 are kept in the string pool or not?
Mushfiq Mammadov wrote:Maybe authors intended to write System.out.println(t1 == t3); on line 7.
[OCP 17 book] | [OCP 11 book] | [OCA 8 book] [OCP 8 book] [Practice tests book] [Blog] [JavaRanch FAQ] [How To Ask Questions] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Roel De Nijs wrote:Secondly, your example is a bit tricky because "string" and "Hello World" are String literals and therefor will be definitely in the pool. But the String objects created by o.toString() and "Hello World".trim() are not String literals and thus will not be in the String literal pool.
Strings are immutable and literals are pooled. The JVM created only one literal in memory. x and y both point to the same location in memory; therefore, the statement outputs true.
Mushfiq Mammadov wrote:According this x and z both point to the same location in memory. Which thing I don't understand correctly?
So "Hello World" has no leadin or trailing white space, thus this string is returned. That's completely similar with the toString method and thus explains why true is printed as well.javadoc trim() method wrote:Returns: A copy of this string with leading and trailing white space removed, or this string if it has no leading or trailing white space.
Roel De Nijs wrote:
javadoc trim() method wrote:Returns: A copy of this string with leading and trailing white space removed, or this string if it has no leading or trailing white space.
Roel De Nijs wrote:That's an excellent question
Have a well-deserved cow!
My favorite is a chocolate cupcake with white frosting and tiny ad sprinkles.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
|