• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Stingbuffer Class...

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can someone pls explain why class D ends with the output "True" but class E with false...
Thanks gokhan
class D
{
public static void main(String[] args) {
String s1 = new String("hello");
String s2 = new String("hello");
if (s1.equals(s2))
System.out.println("equal");
else
System.out.println("not equal");
}
}

class E {
public static void main(String[] args) {
StringBuffer sb1 = new StringBuffer("hello");
StringBuffer sb2 = new StringBuffer("hello");
if (sb1.equals(sb2))
System.out.println("equal");
else
System.out.println("not equal");
}
}
 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, As far as I know StringBuffer didn't override the "equals" method but String did it !
Hope this help
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
StringBuffer does a straight compare equivalent to using ==
 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
even if we do sb1==sb2 it doesnt work,
only way to do is use sb1.toString() and compare.
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is correct.
StringBuffer treats equals() identically to ==
In both cases it is doing a straight Object compare.
 
Gokhan Gultekin
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys it is more clear now . . .
 
Ranch Hand
Posts: 328
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI,
From the JDK Docs;
*******************************
public boolean equals(Object obj)
Indicates whether some other object is "equal to" this one.
The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any reference values x and y, this method returns true if and only if x and y refer to the same object (x==y has the value true).
*************************************
A StringBuffer object is not immutable so even though the content is the same a new object is created when you call the constructor. Therefore the two StringBuffers do not refer to the same object.
With the new String() constructor, if the String is the same as an existing one the compiler uses the existing one as they cannot change - they are immutable.
Hope that helps - and hope that's correct too. :-)
Terry
[This message has been edited by Terence Doyle (edited July 13, 2001).]
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I thought that every time you created a String object with new a new instance was created in literal pool. The reason String equals method returns equal in this case is because String class overrides its equals method. Is my understanding wrong?
Well if you have
String st1 = new String("hello");
String st2 = new String("hello");
then
st1.equals(st2) is true
while st1 == st2 is false

Originally posted by Terence Doyle:
HI,
From the JDK Docs;
*******************************
public boolean equals(Object obj)
Indicates whether some other object is "equal to" this one.
The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any reference values x and y, this method returns true if and only if x and y [b]refer to the same object
(x==y has the value true).
*************************************
A StringBuffer object is not immutable so even though the content is the same a new object is created when you call the constructor. Therefore the two StringBuffers do not refer to the same object.
With the new String() constructor, if the String is the same as an existing one the compiler uses the existing one as they cannot change - they are immutable.
Hope that helps - and hope that's correct too. :-)
Terry
[This message has been edited by Terence Doyle (edited July 13, 2001).][/B]


 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Terence Doyle:
With the new String() constructor, if the String is the same as an existing one the compiler uses the existing one as they cannot change - they are immutable.

The new String() will always generate two different String objects so for:
String s1 = new String("A");
String s2 = new String("A");
// s1 == s2 is always false
When you use this form:
String s1 = "A";
String s2 = "A";
// s1 == s2 is always true

------------------
Co-Moderator of the Programmer Certification Forums
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic