-2

I see that new Arrays are always initialised with default values. For example, an int array will have all its elements initialised as 0 initially. For example:

int myArray = new int[7];
System.out.println(myArray[3]); // 0 

I wanted to ask how arrays of other types (primitives & non-primitives) are initialised and what values do their elements hold initially?

Aaditya Sharma
  • 3,330
  • 3
  • 24
  • 36

1 Answers1

-1

All the arrays of non-primitives types would have their elements initialised as null. And the arrays of primitive types will be initialised as follows:

  • Arrays of type byte, short, int, long will have elements initialised as 0
  • Arrays of type float, double will have elements initialised as 0.0
  • Arrays of type boolean will have elements initialised as false
  • Arrays of type object will have elements initialised as null
  • Arrays of type char will have elements initialised as '\u0000' which is a null character.
Joe
  • 29,416
  • 12
  • 68
  • 88
Aaditya Sharma
  • 3,330
  • 3
  • 24
  • 36
  • You forgot `char`. – MC Emperor Dec 23 '20 at 10:52
  • 2
    Thank you for trying to cover questions and answers that don't exist already on Stack Overflow, but please make sure they truly don't exist. ([What is the default initialization of an array in Java?](https://stackoverflow.com/q/3426843/12323248)) – akuzminykh Dec 23 '20 at 10:53