1

Is there a way to set the default values of an array upon construction? I'm initialising a character array like so,

char[] chars = new char[value];

However, the default value for each element is decimal 0, rather than decimal 48 which is the ASCII character for '0'.

The closest thing I've found is calling:

Arrays.fill(chars, '0');

which fills the array with the desired decimal value 48

But this is called after the array has already been created full of 0 values which presumably takes more time?

5
  • A: For what reason you want to do it? B: You can initialize it with as many values you need and a "default" value like this: char[] c = new char[] { '0' }; but then the size is hard coded Commented Apr 16, 2018 at 13:09
  • If it's a small array of known size, you can use the literal notation as well: char[] chars = {48,48,...} Commented Apr 16, 2018 at 13:10
  • It's part of a method, so I don't want to hard-code the size of the array. Commented Apr 16, 2018 at 13:10
  • @XtremeBaumer yeah I accidentally surrounded by single quotes. Fixed now. Commented Apr 16, 2018 at 13:11
  • Not really, I'm aware of how to declare an array. I wanted to know whether the default value can be adjusted. Turns out it can't. Commented Apr 16, 2018 at 13:43

4 Answers 4

3

As per JLS §15.10.2. Run-Time Evaluation of Array Creation Expressions

...

Then, if a single DimExpr appears, a one-dimensional array is created of the specified length, and each component of the array is initialized to its default value (§4.12.5).

...

and the default value of char primitive is \u0000.

You could use the array initializer syntax as per JLS §10.6. Array Initializers to avoid the reallocation of array elements:

char[] chars = { 'a', 'b', 'c' };

but this will only work if you know the array size during compilation.

Sign up to request clarification or add additional context in comments.

2 Comments

Where does it stated that char primitive default value is zero?
Perhaps I badly worded the answer. The default value of char is null character written as \u0000 (which is 0x00, or just 0) as per docs.oracle.com/javase/specs/jls/se10/html/…
2

If it is array of primitives it is initialized with the default value for that primitive type. For boolean it is false and for numeric types and char it is 0 (the byte value) or '\u0000' if you need the char. If you want to set a default value you will have to fill that array manually the way you have found. I don't think you will notice any performance drops.

5 Comments

Default value for a char is not zero but null. '0' is it's ASCII code.
nope. Default value for Character is null. But he is using 'char' which is a primitive and not an object and default value is 0
Check the documentation. Default value for char is null.
For char its null "character". Not 0 or null.
ok my bad in explanation. We are talking about different stuff then. I meant it is 0 as byte value. It is 0 as in the number 0 not the char '0' (because the '\u0000' actually equals 0). If you create a char a; and do a==0 it will be true
2

For chars, default value is null character, so you get its ASCII code '0'.

5 Comments

for Character (and any object actually) default value is null. He has array of char which is a primitive and cannot be null. default value is indeed 0
Documentation states that "For type char, the default value is the null character, that is, '\u0000'."
null char which is equal to the number 0 (since chars are actually bytes). Not null - the value. I didn't mean the '0' char - that's what the guy asking wants but he gets the number 0 which is the '\u0000'.
@VeselinDavidov "since chars are actually bytes" - From JLS §4.2.1: "For char, from '\u0000' to '\uffff' inclusive, that is, from 0 to 65535" - So no, chars are not bytes.
ok ;) not bytes
1
char[] chars = new char[] {'a', 'b', 'c'};

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.