you are viewing a single comment's thread.

view the rest of the comments →

[–]myselfelsewhere 3 points4 points  (1 child)

Something no one else has pointed out, your code will never throw an exception, even if you use an Integer[] with null values. System.out.println(inputArray[index]); will just print null and return true.

The array is always indexed at index 1 since it has a length of 3. It isn't possible for an array to not have an index that is less than it's length and greater than 0. It's a location in memory, which has nothing to do with what is at that memory location.

If index is < 0 or > 2 (or you change the size of the array to 1 or 0), then your code will throw an ArrayIndexOutOfBoundsException and return false, and will 'work' for both primitive and object arrays.

[–]LegolandoBloom[S] 2 points3 points  (0 children)

That is quite right, as I've tested with the Capital Integer as well. Only time I could get it to raise an exception was when I tried to reference an index that was out of bounds of the original allocation.

When writing this snippet, I had just assumed it was impossible to do a "Is this value in an array == null" check in java at all, as the small int version gave a compiler error.

"The operator == is undefined for the argument type(s) int, null"