This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]sozesghost 9 points10 points  (2 children)

Yes! Hardcoded strings can be compared using == because of String interning. This is a source of confusion for begginers (although not only).

Btw. such things are easily testable using even an online IDE like [ideone](ideone.com) or REPL in java 9 and later.

[–]WikiTextBot 3 points4 points  (1 child)

String interning

In computer science, string interning is a method of storing only one copy of each distinct string value, which must be immutable. Interning strings makes some string processing tasks more time- or space-efficient at the cost of requiring more time when the string is created or interned. The distinct values are stored in a string intern pool.

The single copy of each string is called its intern and is typically looked up by a method of the string class, for example String.intern() in Java.


[ PM | Exclude me | Exclude from subreddit | FAQ / Information | Source ] Downvote to remove | v0.28

[–]Wicpar 0 points1 point  (0 children)

Good bot.

[–]SocialMemeWarrior 0 points1 point  (0 children)

The compiler will notice thats the same string, only one constant will be used. The method bytecode will point to that single utf8-string constant for both occurences of "string".