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?

Sam
  • 922
  • 1
  • 10
  • 23
  • 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 – XtremeBaumer Apr 16 '18 at 13:09
  • If it's a small array of known size, you can use the literal notation as well: `char[] chars = {48,48,...}` – Mena Apr 16 '18 at 13:10
  • It's part of a method, so I don't want to hard-code the size of the array. – Sam Apr 16 '18 at 13:10
  • @XtremeBaumer yeah I accidentally surrounded by single quotes. Fixed now. – Mena Apr 16 '18 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. – Sam Apr 16 '18 at 13:43

4 Answers4

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.

Karol Dowbecki
  • 38,744
  • 9
  • 58
  • 89
  • Where does it stated that char primitive default value is zero? – Emre Dalkiran Apr 16 '18 at 13:20
  • 1
    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 https://docs.oracle.com/javase/specs/jls/se10/html/jls-4.html#jls-4.12.5 – Karol Dowbecki Apr 16 '18 at 13:23
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.

Veselin Davidov
  • 6,886
  • 1
  • 13
  • 21
  • Default value for a char is not zero but null. '0' is it's ASCII code. – Emre Dalkiran Apr 16 '18 at 13:11
  • 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 – Veselin Davidov Apr 16 '18 at 13:12
  • Check the [documentation](https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.12.5). Default value for char is null. – Emre Dalkiran Apr 16 '18 at 13:15
  • For char its null "character". Not 0 or null. – hellzone Apr 16 '18 at 13:21
  • 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 – Veselin Davidov Apr 16 '18 at 13:21
2

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

Emre Dalkiran
  • 381
  • 2
  • 8
  • 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 – Veselin Davidov Apr 16 '18 at 13:13
  • Documentation states that "For type char, the default value is the null character, that is, '\u0000'." – Emre Dalkiran Apr 16 '18 at 13:18
  • 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'. – Veselin Davidov Apr 16 '18 at 13:21
  • @VeselinDavidov "*since chars are actually bytes*" - From [JLS §4.2.1](https://docs.oracle.com/javase/specs/jls/se9/html/jls-4.html#jls-4.2.1): "*For `char`, from `'\u0000'` to `'\uffff'` inclusive, that is, from 0 to 65535*" - So no, `char`s are not `byte`s. – Turing85 Apr 16 '18 at 13:47
  • ok ;) not bytes – Veselin Davidov Apr 16 '18 at 13:47
1
char[] chars = new char[] {'a', 'b', 'c'};
vanje
  • 9,404
  • 2
  • 31
  • 41