Is it possible to assign a "null" value to a single element inside an integer array?
For example:
int[] num = new int[3];
num[0] = 23;
num[1] = null;
num[2] = 12
When I tried this it gives an error viz. incompatible types: <nulltype> cannot be converted to int
But when I tried same in an Object array, I can assign a "null" value to a single element inside an array.
Below is the code:
public class Person {
private String name;
private int age;
public Person(String n, int a){
name = n;
age = a;
}
public static void main(String[] args) {
Person[] p = new Person[3];
p[0] = new Person("John", 17);
p[1] = null;
p[2] = new Person("Mark", 18);
}
}
"null" is allowed in an Object array, while in an int array it is not. Why is that? What's the difference between those two array types? I thought all arrays in Java are of an Object type. Please do correct me if I'm wrong here.