• 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 ...
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

String and StringBuffer

Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am little confused with String and StringBuffers. I can understand equals but not ==.Am reading these topics again and again in K&B.It still didnt answers my questions. So, thought of asking u ppl

My questions are
a. what are the conditions where any string reference variables String s1, String s2 will be equal (s1==s2)
b. what are the conditions where any string reference variables StringBuffer s1, StringBuffer s2 will be equal (s1==s2)

I know there could be 'n' no of situations they may be equal. I like to know generalized condition or concept level to understand when they could be equal.

Hope you ppl understand my question and expecting eye opener answers for this.
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
s1==s2 returns true, if bothe the references (s1,s2) ae aliases (referes to same object)

class Test
public static void main(String args[])
String s1="abcd"; //line1
String s2="abcd"; //line2
String s3=new String(s2);//line3
String s4= new String("abcd");//line4


line1-// a string object "abcd" is created in string pool

line2-// here another string object without using new operator is created so the reference of "abcd" (in string pool) will be given to s2.here s1, s2 are aliases, means pointing to same string object.
****** If 'new' keyword is used to create a new object , then that object is diffent object *********

lines3// here s3 object is created using s2, 'new' keyword is used . so s2, s3 are two different objects, but having same string content. here equals method returns true (contents), == returns false ( not same objects)

line4// a new object s4 is created.

line5// s1,s2 are created with out using 'new' keyword, == returns true ( s1,s2 refers to same string placed in string pool )

line 6// s3 is created using new keyword. so contents are same but objects are different. so == returns false.

line7// s4 is created using new keyword. ..............
line 8 // s3, s4 two different objects so == returns false.

try the same code with equals method as well as == operator for better understanding. Replace all String objects with StringBuffers and run the code.
Hope this helps u
[ December 16, 2005: Message edited by: Naresh Kumar ]
Anjana Ravindran
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your example is gud. I am able to get the concept little. Checking my understanding with StringBuffer.

Creating via new objects will always make == to return false. Since StringBuffers can be created only via new(), no stringbuffer objects will be equal. Am i right?
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just a bit addition. Not sure..
String s4= new String (s3);
String s5= new String(s3);
(s4==s5) would be tru in this case too??

Posts: 15876
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to understand the difference between reference equality and object value equality via equals().

The == operator, when used with object references, tests whether two reference variables refer to the same object. In general, it's incorrect to use == to compare the values of two objects. Use the equals() method instead. Use == to test if two object references are pointing to the same object in memory.

Object x = new Object();
Object y = new Object();
System.out.println(x == y); // false - they refer to different objects

String s = "Hello";
String t = " Hello ".trim();
System.out.println(s == t); // false - they refer to different objects
System.out.println(s.equals(t)); // true - the two objects have equal values

StringBuffer sb = new StringBuffer("Hello");

System.out.println(sb == s); // false - they refer to different objects

// a StringBuffer and a String are never equal() and vice versa,
// doesn't matter if their values are the same
System.out.println(sb.equals(s)); // false

System.out.println(s.equals(sb.toString())); // true
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have my own doubts of String and StringBuffer.
String is ok but why StringBuffer does not override equals() and hashcode() method?

have a look at follwing.

String s1=new String("dilip");
String s2=new String("dilip");

System.out.println(s1==s2); //false
System.out.println(s1.equals(s2)); //true
System.out.println(s1.toString()); //dilip
System.out.println(s1.hashCode()); //95587310

StringBuffer sb1=new StringBuffer("dilip");
StringBuffer sb2=new StringBuffer("dilip");

System.out.println(sb1==sb2); //false
System.out.println(sb1.equals(sb2)); //false
System.out.println(sb1.toString()); //dilip
System.out.println(sb1.hashCode()); //8187137

Q: why hash codes are different in both cases? is it because StringBuffer does not override the hashCode() method? what is the behaviour of hashCode() in Object Class?
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic