By Sam

2018-04-16 13:05:37 8 Comments

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?


@Emre Dalkiran 2018-04-16 13:10:37

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

@Veselin Davidov 2018-04-16 13:13:16

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

@Emre Dalkiran 2018-04-16 13:18:31

Documentation states that "For type char, the default value is the null character, that is, '\u0000'."

@Veselin Davidov 2018-04-16 13:21:00

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'.

@Turing85 2018-04-16 13:47:20

@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.

@Veselin Davidov 2018-04-16 13:47:56

ok ;) not bytes

@Karol Dowbecki 2018-04-16 13:13:02

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.

@Emre Dalkiran 2018-04-16 13:20:10

Where does it stated that char primitive default value is zero?

@Karol Dowbecki 2018-04-16 13:23:07

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…

@Veselin Davidov 2018-04-16 13:09:33

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.

@Emre Dalkiran 2018-04-16 13:11:34

Default value for a char is not zero but null. '0' is it's ASCII code.

@Veselin Davidov 2018-04-16 13:12:32

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

@Emre Dalkiran 2018-04-16 13:15:05

Check the documentation. Default value for char is null.

@hellzone 2018-04-16 13:21:26

For char its null "character". Not 0 or null.

@Veselin Davidov 2018-04-16 13:21:28

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

@vanje 2018-04-16 13:12:39

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

Related Questions

Sponsored Content

33 Answered Questions

[SOLVED] For-each over an array in JavaScript?

22 Answered Questions

21 Answered Questions

[SOLVED] How do I declare and initialize an array in Java?

  • 2009-07-29 14:22:27
  • bestattendance
  • 4087307 View
  • 1810 Score
  • 21 Answer
  • Tags:   java arrays declare

45 Answered Questions

78 Answered Questions

[SOLVED] Is Java "pass-by-reference" or "pass-by-value"?

25 Answered Questions

[SOLVED] How do I determine whether an array contains a particular value in Java?

  • 2009-07-15 00:03:21
  • Mike Sickler
  • 1738824 View
  • 2047 Score
  • 25 Answer
  • Tags:   java arrays

76 Answered Questions

[SOLVED] How do I remove a particular element from an array in JavaScript?

  • 2011-04-23 22:17:18
  • Walker
  • 5592746 View
  • 6930 Score
  • 76 Answer
  • Tags:   javascript arrays

30 Answered Questions

[SOLVED] How to append something to an array?

29 Answered Questions

[SOLVED] Create ArrayList from array

32 Answered Questions

[SOLVED] What's the simplest way to print a Java array?

  • 2009-01-03 20:39:39
  • Alex Spurling
  • 1949247 View
  • 1717 Score
  • 32 Answer
  • Tags:   java arrays printing

Sponsored Content