Beginner
Are there any circumstances where it is OK to use == to test for String equality?
Are there any circumstances where it is OK to use == to test for String equality?
Fergal Crawley wrote:It was my understanding that == should not be used for testing String equality.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Beginner
Fergal Crawley wrote:Thanks for your replies. Given your explanations, I didn't expect the code below to work and output "Equal", but it does. Why is that?
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jesper de Jong wrote:...The Java compiler performs an optimization with string literals. If the same string literal, for example "aa", is used multiple times in the same program, then the compiler makes sure that only one String object is created, which is reused...
Beginner
Fergal Crawley wrote:Thanks for your help Jeff. I felt it was a different question because I used variables second time round.
I wasn't aware that;
Jesper de Jong wrote:...The Java compiler performs an optimization with string literals. If the same string literal, for example "aa", is used multiple times in the same program, then the compiler makes sure that only one String object is created, which is reused...
He puts the "turd" in "saturday". Speaking of which, have you smelled this tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
|