GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes What is the Difference between equals() of Stringbuilder and String? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What is the Difference between equals() of Stringbuilder and String?" Watch "What is the Difference between equals() of Stringbuilder and String?" New topic
Author

What is the Difference between equals() of Stringbuilder and String?

Shivom Shukla
Ranch Hand

Joined: Jul 05, 2012
Posts: 77

Following is the code that confuses me..


Answer is true.
Why??
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7892
    
  21

Shivom Shukla wrote:Why??

1. Look at the API for StringBuilder (←click).
2. Do you see an equals() method?
3. What happens in the absence of an equals() method?

HIH

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Shivom Shukla
Ranch Hand

Joined: Jul 05, 2012
Posts: 77

Hi Winston,

Thanks a lot, your questions really solved my issue. I cannot believe I missed this.

Shivom
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7892
    
  21

Shivom Shukla wrote:Thanks a lot, your questions really solved my issue. I cannot believe I missed this.

Don't worry. It still happens to me - although I suspect it's the Alzheimer's these days.

Winston
Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 867
    
    5
Strange. This line does not even compile for me:
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14193
    
  20

That's because it's not valid Java. You can only use the shortcut {"1,2,4"} notation on a line where you declare as well as initialize a variable. You cannot use it for assignment to a variable that was declared earlier.

So it should be:

And ofcourse, you'll get a String array that has one element that contains "1,2,4" - not three elements "1", "2" and "4".


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Shivom Shukla
Ranch Hand

Joined: Jul 05, 2012
Posts: 77

My bad... That is absolutely correct what you have said.
Thanks for pointing it out.
 
GeeCON Prague 2014
 
subject: What is the Difference between equals() of Stringbuilder and String?