-3

I made the following array:

int[] array = new int[9];

This array is initialized, but empty.

In that case, what is the value of any index of this array?

When I print it out, it gives me 0.
Does that mean there is a zero stored in here (like if you had called int x = 0)?
Or is it null?

Also, is this the same for any Object array?
Is it an empty instance of this object, or is it null?

Snostorp
  • 544
  • 1
  • 8
  • 14
cvbattum
  • 811
  • 2
  • 15
  • 32
  • all 1s. This is basic Java in any 101 book or tutorial. Have you seen the ones on Oracle? http://goo.gl/QiHpsb – tgkprog Mar 07 '15 at 21:36
  • 4
    I'm voting to close this question as off-topic because it can be trivially found in the documentation. http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html – Jeroen Vannevel Mar 07 '15 at 21:37
  • @JeroenVannevel are general purpose questions not allowed on SnackOverflow; if so, how would we flag them? – Obicere Mar 07 '15 at 21:49
  • possible duplicate of [What is the default initialization of an array in Java?](http://stackoverflow.com/questions/3426843/what-is-the-default-initialization-of-an-array-in-java) – Alexis King Mar 07 '15 at 23:24
  • The array is not 'empty', it is full with 9 instances of zero. – Arfur Narf May 11 '23 at 11:41

6 Answers6

2

A copy from Oracle Java Documentation.

Primitive Data Types - Default Values:

Data Type   Default Value (for fields)
byte        0
short       0
int         0
long        0L
float       0.0f
double      0.0d
char        '\u0000'
String      null
Object      null
boolean     false

So, for int, it is zero (0).

Snostorp
  • 544
  • 1
  • 8
  • 14
adrCoder
  • 3,145
  • 4
  • 31
  • 56
1

If it is an Array of objects it will be initialized with null, if it's an array of primitive values (like int) it will be initialized with 0. For the non-number primitive boolean it is false and for 'char' it is '\u0000'. For more information, check the table Default Values on the following page:

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

Marv
  • 3,517
  • 2
  • 22
  • 47
0

All elements are initialized to 0.

Emil Laine
  • 41,598
  • 9
  • 101
  • 157
0

Integer class can be null however "int" type cannot be null.

You can try int x = null; you'll have an error.

Therefore it is created by default to 0 until you change it.

Arnaud Bertrand
  • 1,341
  • 10
  • 13
0

Every value in an int array is initialized to 0 by default. This is something that you can easily try on your own:

    int [] nums = new int [9];

    for (int i = 0; i < nums.length; i++) {
        System.out.println(nums[i]);
    }

Result:

0
0
0
0
0
0
0
0
0
Zarwan
  • 5,537
  • 4
  • 30
  • 48
0

Here are the default values for the different types in Java:

byte 0

short 0

int 0

long 0L

float 0.0f

double 0.0d

char ‘u0000′

String (or any Object) null

boolean false

As you can see, int defaults to 0.

Dermot Blair
  • 1,600
  • 10
  • 10