Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • 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
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Question on String Buffer

 
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
 
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
 
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?
 
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?
 
Sheriff
Posts: 9674
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • 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

 
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: 9674
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • 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 ==...
 
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: 9674
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • 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
 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic