-5

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?

Cœur
  • 34,719
  • 24
  • 185
  • 251
cvbattum
  • 791
  • 2
  • 13
  • 31
  • 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

6 Answers6

0

All elements are initialized to 0.

emlai
  • 39,703
  • 9
  • 98
  • 145
0

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,422
  • 2
  • 22
  • 46
0

I copy from Javadocs:

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

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 (or any object)      null
boolean false

So, for int, it is zero (0)

adrCoder
  • 2,975
  • 2
  • 25
  • 49
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,091
  • 9
  • 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,167
  • 4
  • 28
  • 46
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,530
  • 10
  • 10