Indeed. A primitive always holds a value of that type, there is no special "null" value. With references the value they hold is actually the address of an object in memory. A special null value is necessary to indicate that a reference points at nothing. For example, this value might be 0 because no object can ever be allocated to the address 0. You never see what the underlying value is, you just get told it's null. If the JLS were to have primitives accept null they would have to set aside a special value for it, which would be silly and meaningless.
Joined: Jul 15, 2003
By the way, when you create an array the JVM allocates enough memory for the entire array. Every index has something in it. In the case of references the value is the special null value. In the case of booleans it is the value for false. In the case of int, short, etc. the value is 0. Your design seems odd, but I suspect you're going to want to keep a "count" to know where in your array you are rather than looking for the first index without a value assigned.
Originally posted by Tony Morris: I felt obliged to point out that null is not necessary, but it is "just how Java does it". There are much more elegant solutions to the problems that null is used to solve in language theory.
Not that I would know what the more elegant solutions are or whether or not they fulfill the requirements Java had to but I should have said "used" instead of "necessary" nonetheless.
Or perhaps you shouldn't use an array at all, but a collection that grows when you put things into it?
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Originally posted by Ilja Preuss: Or perhaps you shouldn't use an array at all, but a collection that grows when you put things into it?
Yeah, it's hard to tell from the original code snippet what the intent is. As Ken pointed out above, the "design seems odd," and Ilja offers an excellent suggestion. I suggest taking a step back and reconsidering the approach here.