hello there, is there anyone who can tell me hwo to actually check whether an an int array element is a null or not? in java, I tried (array[i]==null) or (array[i].equals("")) but it seems that the compiler will tell me that int is incomparable to null or string everytime I used those two to check whether my int array element is a null or not. Besides, I also used (array[i]==0), but this cant really check whether the int array element is null or not right? So I am wondering is there anyone that can help me to solve this problem.

5 Years
Discussion Span
Last Post by stultuske

an int is a primitive, not an object. an int's default value is 0. of course, the array itself is an object, so you can check whether or not the array is null.

since an int cannot be null, no, you can't, but, if for instance, your elements are limited to a certain group of values, let's say, only the positive numbers, you can initialize your array and give all the elements a default value of -1, so, you can check whether the element == -1 or not.

if this is not enough, create an array of Integers instead of an array of ints.


Create an array of Integers instead of an array of ints. Java 1.5+ supports autoboxing and unboxing so an array of Integer will accept int values.


it won't 'accept int values', it will, however, through the autoboxing mechanism you've mentioned, transfer int variables to Integers, which then are accepted.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.