Win a copy of Pro Spring MVC with WebFlux: Web Development in Spring Framework 5 and Spring Boot 2 this week in the Spring 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

What would be the output?

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What would be the output of the following code snippet?


StringBuffer sb1 =new StringBuffer("Neha");
StringBuffer sb2 =new StringBuffer("Neha"):
String s1 = "Neha";
System.out.println(sb1==sb2);
System.out.println(sb1.equals(sb2));
System.out.println(sb1.equals(s1));
 
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

neha

false false false

Thanks
Anil Kumar
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The tricky part is that StringBuffer is not overriding the "equals" method
 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1st ans: false
reason: as both are different objects.

2nd Ans: false
reason: The StringBuffer class does not override the equals methos.so, by default, it will check the reference. not the object content.

3rd ans: false
reason: Here, the s1 is the object of String not the object of StringBuffer class.

I think you got the point.

thanks&Regards,
Krishna
SCJP1.4
SCWCD1.4
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If this

was

then will return as string objects reside in a pool. When ever a string object is to be created, string pool is checked for an existing object of same value, if exists the reference is assigned to existing object else a new object will be created and the reference assigned.

P.S. Please correct if something is wrong.
 
anil kumar
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

Ravi
--------------------------------
String sb1 =new String("Neha");
String sb2 =new String("Neha");
(sb1==sb2)
true
--------------------------

No Ravi that is not the case.

when ever you say new a new object is created in heap.

when you give literal ,then the String pool will come into play.

Say for ex
String sb1="anil";//line 1
String sb2="anil";//line 2

at line 1.one object is created at heap and one at String constant pool.
at line 2.Because at String pool already that string is there so the reference of that will assigned to sb2.


now sb1==sb2 will return true.

Thanks
Anil Kumar
[ May 21, 2007: Message edited by: anil kumar ]
 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow. StringBuilder /-Buffer don't override equals? Why not?
I have never tried it out, as all the collections, the String-, the Wrapper-classes, all the classes I expected to overwrite equals did, I have been sure the SB-classes will also do it.
Anyway, thanks for clarifying this!
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Neha,
Thanks for posting a question which let us know one good thing. "StringBuffer does NOT override equals() method!".

Thanks to chinnikumar for pointing out that too. I was also in the same boat as that of Sasha Ruehmkorf !

Ravi, as anil said, until and unless you invoke *new* string, the String references will be true only! Once you invoke a *new* String, whatsoever it be, a *fresh and brand new object* will be created on Heap!

Now you can check your answer!

HtH.
[ May 21, 2007: Message edited by: Raghavan Muthu ]
 
Neha Monga
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks all!

The above explanations help.
 
Men call me Jim. Women look past me to this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic