not sure if I am asking this correctly, but does anyone know if List<Character> and List<String> would hold different amount of memory space for one-character elements, that is, character elements or strings with only one character each?
I would expect List<Character> to hold less memory space but since Character is a class and not the primitive char, I wonder if List<Character> would then hold the same or different amount of space as List<String>.
The List
istself only contains references in both cases, so it is the same size.
This does not mean that there will not be any effect on overall memory one way or the other, but this will depend on the rest of your application.
The values might already exist in memory anyway, and depending on their values and how they are used elsewhere, caching may be involved. Strings might be interned, and frequent Characters can also be cached by the implementation, e.g. if they are created with valueOf()
:
This method will always cache values in the range '\u0000' to '\u007F', inclusive, and may cache other values outside of this range..