0

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.

Abhinav
  • 530
  • 8
  • 21
robert
  • 249
  • 4
  • 11
  • 1
    Try to use Integer type instead of int. These wrapper types are capable of null values, primitive types like int are not. – Andreas Nov 08 '18 at 06:06
  • Object value can be null but int primitive type can't be null. so to solve this issue yo have to make Integer[] num = new Integer[3]; – GauravRai1512 Nov 08 '18 at 06:09
  • In Java programming, null can be assigned to any variable of a reference type (that is, a non-primitive type) to indicate that the variable does not refer to any object or array. – Abhinav Nov 08 '18 at 06:14
  • I'm a little bit confused because it says in this post that primitive arrays and reference arrays are similar.. Please enlighten me about this... https://stackoverflow.com/questions/15060584/what-is-the-difference-between-primitive-array-and-array-of-reference – robert Nov 08 '18 at 06:14
  • @robert Do have a look here: https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.3.1 – Abhinav Nov 08 '18 at 06:44
  • Just a quick question, it's a little bit different from my posted question. I can print the values in Integer[] even if it has a null value but on the Object `Person` it throws an error `nullpointerexception` when I print it. `System.out.println(p[1])` How can I fix this? – robert Nov 08 '18 at 07:40

7 Answers7

2

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.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • I'm a little bit confused because it says in this post that primitive arrays and reference arrays are similar.. Can you please enlighten me about this... https://stackoverflow.com/questions/15060584/what-is-the-difference-between-primitive-array-and-array-of-reference – robert Nov 08 '18 at 06:11
  • @robert I also don't really follow that question or either of the answers. But what I said regarding `int` and `Integer` basically holds true for arrays of the same. – Tim Biegeleisen Nov 08 '18 at 06:14
  • Oh i see. Thanks.. – robert Nov 08 '18 at 06:17
  • @robert Arrays are arrays, there is no such thing as primitive arrays. There is, however, arrays of a primitive type, which behaves just like arrays of object/reference type. All Java arrays have to abide with the rules of the type of elements it holds. Array of primitive `int` has to hold primitive `int`, you can't assign `null` to an element of the array when primitive `int` can't hold `null`. – Jai Nov 08 '18 at 06:37
  • "The boxing rules in Java make it fairly easy to use int and Integer interchangeably, most of the time" perhaps the most notable example where you can't is that `int[]` and `Integer[]` are not interchangeable. – Andy Turner Nov 08 '18 at 07:06
  • Just a quick question, it's a little bit different from my posted question. I can print the values in Integer[] even if it has a null value but on the Object `Person` it throws an error `nullpointerexception` when I print it. `System.out.println(p[1])` How can I fix this? – robert Nov 08 '18 at 07:39
1

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 person can be null but age cannot be null since age is primitive type

noone
  • 6,168
  • 2
  • 42
  • 51
0

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.

Mustafa Çil
  • 766
  • 8
  • 15
0

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/

0

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.

Abhishek
  • 688
  • 6
  • 21
0

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

shu cheng
  • 1
  • 1
-1

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.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Saurabh Oza
  • 169
  • 4
  • 18