Hey, I have this code that checks any String for a palindrome, wondering why The one where I use just StringBuilder doesn't work as intented? it always returns equal, is that because the method I use in the constructor affects the initial string as well?
#StringBuilder comparison
13 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @molten linden! Please use
/closeor theClose Postbutton above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
you're using StringBuilder in both though? im confused
Strings are immutable, you can't change them
StringBuilder is not, it's mutable, and reverse mutates the content
Yes, however if you notice in the first example I compare Strings and in the second I compare StringBuilders
ah
i mean your 2 snippets just logically can't coexist
from your first snippet, sb.reverse must be modifying sb
but you seem to have just... forgotten that in the second snippet
that's what I was asking... "is that because the method I use in the constructor affects the initial string as well?" so does it happen than sb 2 is given the value of the first sb, but at the same time sb stores its new value?
yes, otherwise your first snippet wouldn't work as sb.reverse() wouldn't affect sb in that scenario