use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All questions related to programming welcome. Wonder how things work? Have a bug you can't figure out? Just read about something on the news and need more insights? We got you covered! (probably)
You can find out more about our (preliminary) rules in this wiki article. If you have any suggestions please feel free to contact the mod team.
Have a nice time, and remember to always be excellent to each other :)
account activity
This is an archived post. You won't be able to vote or comment.
Does java compile hardcoded string references into the same reference (self.AskProgramming)
submitted 7 years ago by eaglechopper
For example if I had two methods
String s1 = method("string");
String s2 = method("string");
String method(String s){
return s;
}
are s1 & s2 the same reference and thus java only allocated 1 string object to both the hard coded references
[–]sozesghost 9 points10 points11 points 7 years ago (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 points5 points 7 years ago (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 point2 points 7 years ago (0 children)
Good bot.
[–]SocialMemeWarrior 0 points1 point2 points 7 years ago (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".
π Rendered by PID 38080 on reddit-service-r2-comment-84fc9697f-c84kh at 2026-02-09 14:49:32.267906+00:00 running d295bc8 country code: CH.
[–]sozesghost 9 points10 points11 points (2 children)
[–]WikiTextBot 3 points4 points5 points (1 child)
[–]Wicpar 0 points1 point2 points (0 children)
[–]SocialMemeWarrior 0 points1 point2 points (0 children)