Timeline for Is it a good idea to use strings in a struct as values to static properties?
Current License: CC BY-SA 4.0
12 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Nov 25 at 22:36 | comment | added | Alexander |
@DaveBlack Oh I think we're using the terms differently. By "string constant" I mean something like the FOO in static final var FOO = "abc"; . It's one of the GC "roots", and will never be GC'ed. It might even be stored in a separate memory region, where it doesn't even have to be scanned+marked by the GC.
|
|
Nov 25 at 20:29 | comment | added | Dave Black | @Alexander Yes. Strings that are explicitly defined in code (via use of quotes) are interned. All strings in C# are immutable. My point is that there is a difference between marking something (even a string) as static (which the OP has done) vs. marking it as constant - e.g. a static string's reference can be modified by pointing to a new string, whereas a constant string cannot be changed in any way. | |
Oct 22, 2023 at 22:46 | comment | added | Alexander | @DaveBlack in both C# and Java, string constant are interned and immutable. | |
Oct 22, 2023 at 18:38 | comment | added | Dave Black |
The other thing is that the strings the OP describes are defined as static , not const . Those properties would not be treated as const as you describe.
|
|
Oct 22, 2023 at 18:35 | comment | added | Dave Black | You were describing the behavior of constant (compile time) strings. This is very different than runtime behavior of dynamic string creation. Strings are immutable and any change to a string creates a new one. | |
Oct 20, 2023 at 21:54 | comment | added | Alexander | @DaveBlack Hmm? I was specifically talking about string constants. | |
Oct 20, 2023 at 21:51 | history | edited | Alexander | CC BY-SA 4.0 |
edited body
|
Oct 20, 2023 at 21:15 | comment | added | Dave Black | When you say strings are not on the heap and not subject to garbage collection, this is not correct. You are confused between constant strings (which are interned to a static table in the program) and dynamic/runtime created strings (which ARE created on the heap and subject to GC). I have debugged many a memory dump for apps with memory problems caused by inefficient string creation. | |
Dec 21, 2020 at 14:45 | history | edited | Alexander | CC BY-SA 4.0 |
added 20 characters in body
|
Jan 23, 2019 at 8:50 | comment | added | richzilla |
@Hangman4358 Thats correct in C# aswell, but these arent declared as const , the values are all calculated at runtime
|
|
Jan 22, 2019 at 22:48 | comment | added | Hangman4358 | Don't know about C# but in Java constant strings (Java final) are actually inlined so often the class containing them isn't even loaded. (Also makes for some extra funky things that can happen with reflection if you are not careful) | |
Jan 22, 2019 at 21:38 | history | answered | Alexander | CC BY-SA 4.0 |