aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question on String Buffer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question on String Buffer" Watch "Question on String Buffer" New topic
Author

Question on String Buffer

Jyothsna Panchagnula
Ranch Hand

Joined: Jul 11, 2005
Posts: 113
Source : Poddar's mock questions



My answer is false,true,false,dar.
but the answer is "a".
I have a question here , when we compare 2 different stringbuffer objects using quals , why is the result not true?

any inputs on this is appreciated

thanks,
Jyothsna
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
I have a question here , when we compare 2 different stringbuffer objects using quals , why is the result not true?


As StringBuffer/StringBuilder does not override equals() method, so here Object class' equals() method is called, that uses == operator to compare two objects. Now you know everything.


SCJP 6
Jyothsna Panchagnula
Ranch Hand

Joined: Jul 11, 2005
Posts: 113
Puneet,

Thanks ..I missed it out
Sushant Kaushik
Greenhorn

Joined: Dec 12, 2008
Posts: 25
Punit, Can you please explain why sb1.equals(ss1) will return false. For sb1 also one String object "Amit" will be created and placed in String pool, so ss1 should point to same String Object from the pool, isn't it?
Treimin Clark
Ranch Hand

Joined: Nov 12, 2008
Posts: 757
Originally posted by Jyothsna Panchagnula:
Source : Poddar's mock questions


What is Poddar's mock questions?
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

Originally posted by Sushant Kaushik:
Punit, Can you please explain why sb1.equals(ss1) will return false. For sb1 also one String object "Amit" will be created and placed in String pool, so ss1 should point to same String Object from the pool, isn't it?


It's not about the string pool. The equals method of string class will always return false if the argument passed to it is not String. The equals method of String class looks like this



SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

Originally posted by Ankit Garg:


It's not about the string pool. The equals method of string class will always return false if the argument passed to it is not String. The equals method of String class looks like this



System.out.println(sb1.equals(ss1));

Doesn't this equals() method belong to StringBuffer class,because sb1 is StringBuffer


SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

Sorry! I didn't see the code completely. Now the question seems to be really interesting. But it is simple anyway. The equals method of Object class will be executed. So it will check for ==. But a StringBuffer having the same value as a String will not be == compliant. You don't need to go into the String pool details. Even this would return false

String s = "whatever";
StringBuffer sb = new StringBuffer(s);
System.out.println(sb==s);

The types are different. So there is no way that a StringBuffer and a String are ==...
Preethi Dev
Ranch Hand

Joined: Sep 07, 2008
Posts: 265
Hi Ankit,

String s = "whatever";
StringBuffer sb = new StringBuffer(s);
System.out.println(sb==s);// this will give compiler error since both are
incomparable types.

it wont give false.

Thanks
Preetha.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

Sorry Again. I was just trying to depict the functionality of the equals method of Object class . Let me modify the code

String s = "whatever";
StringBuffer sb = new StringBuffer(s);
System.out.println(((Object)sb)==((Object)s));
Preethi Dev
Ranch Hand

Joined: Sep 07, 2008
Posts: 265
System.out.println(((Object)sb)==((Object)s);// gives false.
this is because we are casting String and StringBuufer to object.
thanks Ankit..i got to know something...
i thought they both cant be comparable in any way...

Thanks again
Preetha
 
Don't get me started about those stupid light bulbs.
 
subject: Question on String Buffer