This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
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: 74

Following is the code that confuses me..


Answer is true.
Why??
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7029
    
  16

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?
Artlicles by Winston can be found here
Shivom Shukla
Ranch Hand

Joined: Jul 05, 2012
Posts: 74

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: 7029
    
  16

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: 865
    
    5
Strange. This line does not even compile for me:
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13868
    
  10

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 7 API documentation
Scala Notes - My blog about Scala
Shivom Shukla
Ranch Hand

Joined: Jul 05, 2012
Posts: 74

My bad... That is absolutely correct what you have said.
Thanks for pointing it out.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is the Difference between equals() of Stringbuilder and String?
 
Similar Threads
Related to String
== with Strings is giving a different result
Strings
solve StringBuilder problem
Sort an array alphabetically and by length