aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String and StringBuffer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String and StringBuffer" Watch "String and StringBuffer" New topic
Author

String and StringBuffer

Anjana Ravindran
Ranch Hand

Joined: Aug 22, 2005
Posts: 76
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.
Naresh Gunda
Ranch Hand

Joined: Oct 15, 2005
Posts: 163
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
System.out.println(s1==s2);//line5
System.out.println(s1==s3);//line6
System.out.println(s1==s4);//line7
System.out.println(s3==s4);//line8
}
}

output
true
false
false
false

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
Regards
Naresh
[ December 16, 2005: Message edited by: Naresh Kumar ]
Anjana Ravindran
Ranch Hand

Joined: Aug 22, 2005
Posts: 76
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?
Balaji Sampath
Ranch Hand

Joined: Sep 30, 2005
Posts: 63
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??

Thanks
Balaji.S
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 5019
    
    8

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


Junilu - [How to Ask Questions] [How to Answer Questions]
vipul patel
Ranch Hand

Joined: Oct 16, 2005
Posts: 146
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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String and StringBuffer