How to assign a "null" value to a single element in an int[] array in Java

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.

8 answers

  • answered 2018-11-08 06:06 NuOne T Attygalle

    In int array all the elements enitialize to 0 whether you have assigned a value or not. its because array is primitive type.

    public class HelloWorld{
    
         public static void main(String []args){
            System.out.println("Hello World");
            int [] a = new int[10];
    
            for(int i=0; i<10; i++)
                System.out.println(a[i]+"");
         }
    }
    

    when you are initialize a Person object, Person is not a primitive type. so variable can be null Thats why in Person class age could be null

  • answered 2018-11-08 06:06 Nicholas K

    The difference here is that an int is a primitive type, meaning that it is not an object and hence it can't ever a store null. You can use the wrapper class Integer to store null.

    Person on the other hand is an object and hence can store a null.

  • answered 2018-11-08 06:06 Mustafa Çil

    int is a primitive type like boolean, byte, short, char, long, float, double and can not be null. but p is a reference type and can be null. null means that there is a reference but don't show an object.

  • answered 2018-11-08 06:07 Tim Biegeleisen

    Primitive Java integers cannot be null, but the Integer class, which wraps a primitive int can be null. Here is one option to consider:

    Integer[] num = new Integer[3];
    num[0] = 23;
    num[1] = null;
    num[2] = 12;
    

    The boxing rules in Java make it fairly easy to use int and Integer interchangeably, most of the time.

  • answered 2018-11-08 06:08 Deepak Gunasekaran

    int is a Primitive type and its not an Object. You can assign null to only Objects whereas primitive types can be assigned with its own default values http://www.c4learn.com/java/java-default-values/

  • answered 2018-11-08 06:11 Abhishek

    null is an object in Java which stores nothing. So you cannot assign an object to either of the primitive types in java like int, double, float.

    Though Java gives wrapper classes to wrap primitive types as well as objects within it. Like Integer, Float, Double etc wherein you can have null objects.

  • answered 2018-11-08 06:13 Saurabh Oza

    Java is not 100% Object Oriented language and the reason is Primitive data types. Primitive data types are not objects and there for they can not hold the null value. Primitive data types should contain some value. In your case int is a primitive data type and Person is not so Person can hold the null value.

  • answered 2018-11-08 06:21 shu cheng

    int is a primitive type in Java, it is not an object.If you want to assign null, than try to use Integer what is its wrapper class