Varun
Varun

Reputation: 3311

Explicitly setting NULL value to an array element

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?

Upvotes: 7

Views: 56041

Answers (9)

Ahmed Mujtaba
Ahmed Mujtaba

Reputation: 764

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 

Upvotes: 1

Pedro Salas
Pedro Salas

Reputation: 1

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);
    }

Upvotes: 0

rohit agrawal
rohit agrawal

Reputation: 23

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

The rest should work as is.

Upvotes: 1

Rudra
Rudra

Reputation: 1688

     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.

Upvotes: 2

Ruchira Gayan Ranaweera
Ruchira Gayan Ranaweera

Reputation: 35557

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.

Upvotes: 0

Bohemian
Bohemian

Reputation: 424993

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.

Upvotes: 1

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.

Upvotes: 10

tckmn
tckmn

Reputation: 59273

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.).

Upvotes: 3

rocketboy
rocketboy

Reputation: 9741

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.

Upvotes: 5

Related Questions