What are the default values of the char array in Java?
It's the same as for any type: the default value for that type. (So the same as you'd get in a field which isn't specifically initialized.)
The default values are specified in JLS 4.12.5:
For type char, the default value is the null character, that is,
'\u0000'
.
Having said that, it sounds like really you want a List<Character>
, which can keep track of the actual size of the collection. If you need random access to the list (for example, you want to be able to populate element 25 even if you haven't populated element 2) then you could consider:
- A
Character[]
usingnull
as the "not set" value instead of'\u0000'
(which is, after all, still a character...) - A
Map<Integer, Character>
- Sticking with
char[]
if you know you'll never, ever, ever want to consider an element with value'\u0000'
as "set"
(It's hard to know which of these is the most appropriate without knowing more about what you're doing.)