• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Shivom Shukla
Ranch Hand
Posts: 77
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Following is the code that confuses me..


Answer is true.
Why??
 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Shivom Shukla
Ranch Hand
Posts: 77
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Winston,

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

Shivom
 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 981
5
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Strange. This line does not even compile for me:
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15481
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".
 
Shivom Shukla
Ranch Hand
Posts: 77
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My bad... That is absolutely correct what you have said.
Thanks for pointing it out.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic