7

Can someone tell me why am I getting compilation error for explicitly setting NULL value to an array element?

int[] a = new int[5];

a[0] = 1;
a[2] = 'a';
a[3] = null; //Compiler complains here


for (int i : a) System.out.println(i);

I am assuming because its an int array and the literal value allowed is 0 and not NULL. Am I right?

Ivar
  • 6,138
  • 12
  • 49
  • 61
UnderDog
  • 3,173
  • 10
  • 32
  • 49
  • 1
    It's because int is a primitive. Should work with "Integer" in place of int – d'alar'cop Aug 18 '13 at 07:36
  • DUPLICATE: http://stackoverflow.com/questions/11047276/null-for-primitive-data-types – d'alar'cop Aug 18 '13 at 07:36
  • Hint: what would you expect to happen if you wrote `int x = null;`? If you understand why that's not allowed, the array case is exactly the same. An array is just a lot of variables that you can access by index. – Jon Skeet Oct 13 '20 at 18:15

9 Answers9

9

Correct. int is a primitive type, which means that it contains an explicit value (a number from -2^31 to 2^31-1), and not a reference, so it can't be null. If you really need null values, use Integer.

chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152
5

Your array

int[] a

is of primitive type int. Primitives cannot have a null value but instead have a default value of 0. Only objects in java can have null as a value. Primitives include: byte,short,char,int,long,float,double.

rocketboy
  • 9,573
  • 2
  • 34
  • 36
3

I am assuming because its an int array and the literal value allowed is 0 and not NULL. Am I right ?

Yes.

If you want to be able to use null, make it an Integer[]. Integers are objects, which can be set to null, unlike primitives (int, char, etc.).

tckmn
  • 57,719
  • 27
  • 114
  • 156
2
     int[] a = new int[5];

The declared array is of type int which is a primitive type like byte, short, long, float, double, char and boolean. Primitives cannot have a null value but instead have their default value as given below

    byte = 0; 
    short = 0;
    int = 0;
    long = 0l;
    float = 0.0f
    double = 0.0d
    boolean = false;

The range formula for the primitive types is mentioned below. Primitives can accept only values falling into this range.

    -2^(N - 1) to 2^(N - 1)-1 
    where N stands for no. of bits that each primitive type takes

Only objects in java can have null as a value. If anyways you want so, better use wrapper classes like

    Byte, Short, Integer, Long, Character, Boolean, etc.
Rudra
  • 1,678
  • 16
  • 29
1

int is a primitive type, which can't be null - it must have a value.

The only option is to set it to a value you can treat like "null", for example -1.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
1

Declare the array as: Integer[] a = new Integer[5];

The rest should work as is.

0

This is a int array. default value of int isn't null an default value is 0, If this is an Integer array then you can set null.

Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
0

An Integer array elements are value types (Int type) so they store values assigned to them in memory locations. If you want to fake a null value, you could try to assign a -1 value to the elements.

Try something like this...

public static void main(String[]args){
        int a[][]={{4,3,2,1},{0,-1,-1,-1},{0,-1,-1,-1}};
        printStatus(a);
        procesar(a);
        printStatus(a);
    }

    public static void procesar (int a[][])
{
int temp, tope0, tope1, tope2;
tope0 = ((a[0].length)-1);
tope1 = 0;
tope2 = 0;

while(a[0][tope0] >= 0){

    if(a[2][tope2]==0){
        System.out.println(a[2][tope2]);
        temp=a[0][tope0];
        a[2][tope2] = temp;
        a[0][tope0] = 0;    
        tope0= tope0 -1;    
        tope2 = tope2 +1 ;
        System.out.println(a[0][tope0]+ " "+a[1][tope1] +" "+ a[2][tope2 -1] );
        printStatus(a);
    }
    System.out.println(a[0][tope0]+ " "+ a[2][(tope2-1)] );
    if((a[2][(tope2 -1)]> a[0][tope0])){
        temp=a[0][tope0];
        a[2][tope2] = temp;
        a[0][tope0] = 0;    
        tope0= tope0 -1;    
        tope2 = tope2 +1 ;
        System.out.println(a[0][tope0]+ " "+a[1][tope1] +" "+ a[2][tope2 -1] );
        printStatus(a);
    }

    if(a[1][tope1]==0){
        System.out.println(a[1][tope1]);
        temp = a[0][tope0];
        a[1][tope1]= temp;
        a[0][tope0]= 0;
        tope0= tope0 -1;
        tope1 = tope1 +1;
        System.out.println(a[0][tope0]+ " "+a[1][tope1 - 1] +" "+ a[2][tope2 -1] );
        printStatus(a);
    }
    System.out.println(a[0][tope0]+ " "+ a[1][(tope1-1)] );
    if(a[1][(tope1-1)]> a[0][tope0]){
        temp = a[0][tope0];
        a[1][tope1]= temp;
        a[0][tope0]= 0;
        tope0= tope0 -1;
        tope1 = tope1 +1;
        System.out.println(a[0][tope0]+ " "+a[1][tope1 - 1] +" "+ a[2][tope2 -1] );
        printStatus(a);
    }
0

Array of Integer can never be null.

However ,Hashmaps are can have null value if the key is not present.

HashMap<String, String> capitalCities = new HashMap<String, String>();

// Add keys and values (Country, City)
capitalCities.put("England", "London");
capitalCities.put("Germany", "Berlin");
capitalCities.put("Norway", "Oslo");
capitalCities.put("USA", "Washington DC");


System.out.println(capitalCities.get("Ukraine"));  // this will return null 
Ahmed Mujtaba
  • 744
  • 5
  • 16