Contents of array of primitive type
I am trying to find out if an int array contains NULL or 0 (the value) when it is first declared and instantiated with 5 elements.
int[] aray =newint[5];
System.out.println(aray[3]);
Output:
0
Question: does the output mean that the value is assigned to all the elements when the identifier aray is assigned to the new array?
Then how can we find out if the array contains NULL or otherwise.
Thanks.
Well you kind of answered your own question. If you looped over the array and printed out each element of the array you will see what is stored as a default value. Try this.
boolean[] bools = new boolean[5];
for(int index = 0; index < bools.length; index++) {
System.out.println(bools[index]);
}
int[] aray = new int[5];
does the output mean that the value is assigned to all the elements when the identifier aray is assigned to the new array?
Answer: if it is an array of any primitive type, all array elements are initialized to its default value. if it is an array of any reference type, all array elements are initialized to null.
try following
Integer[] aray = new Integer[5];
Thanks everyone, got that part.I read somwhere that at the point when the (primitive) array is declared ie. int[] aray; since the identifier aray does not point to anything, "it is" NULL in concept.Thanks everyone!