+ 1

How to make int variable null?

Hey. I saw a question on this site and thought I would do it myself. The question was something of the lines of finding a value in an array 1-100( a value is missing and you should find it). This is what I did; int [] Numbers= new int [100]; Numbers[2]=4; \\ I want to change this address to null int [] NumbersComparison=new int[100]; for(int Counter=1;Counter<Numbers.length;Counter++){ if(Numbers[Counter] !=NumbersComparison[Counter]){ System.out.println(Numbers[Counter]); } } I made 2 arrays. One array has all the values from 1-100 and the other has one value that is different than what it should be. I then used a loop to go through the array with one value changed and said:if that value does not equal the value in the same address in the other array,display the value that is incorrect. Anyway, to my question ,how do I set a value to nul/nothing ? I tried using the keyword "null" but that does not work.

1st May 2017, 10:23 PM
Mogammad Shameer Losper
Mogammad Shameer Losper - avatar
3 Réponses
+ 6
int doesnt count as null, int is 0, char can be counted as a number, ASCII chars for example 1: <blank>
2nd May 2017, 1:39 AM
Complex
Complex - avatar
+ 4
I'm not sure about Java, but in C# you can specify a nullable int by doing something like: int? myint Might be worth a try.
2nd May 2017, 1:32 AM
Mike
Mike - avatar
+ 3
yea it doesnt work. int cannot be set to null. maybe set aside a special value for the one that should be null. like 0 or -1 or maybe Integer.MinValue you can do that or instead change int [ ] to Integer [ ]. Since Integer is an object, it can be set to null.
1st May 2017, 10:29 PM
Edward