Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on String Buffer

 
Jyothsna Panchagnula
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jyothsna Panchagnula
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Puneet,

Thanks ..I missed it out
 
Sushant Kaushik
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 757
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jyothsna Panchagnula:
Source : Poddar's mock questions


What is Poddar's mock questions?
 
Ankit Garg
Sheriff
Posts: 9528
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ankit Garg
Sheriff
Posts: 9528
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 265
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9528
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 265
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic