Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

while NULL?

 
Sam Bluesman
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have array indexes which have nothing i.e. they have null value. I want to make sure that i do not want to put an element in a memory location which has something in it. The code i tried is below



however. It says null can not be used. Without giving u the entire program this may seem a strange thing to want to do so dont ask why! lol.

But was windering what i need to do to do the equivalent
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The default initialization is null only for object types. If the array holds primitive types, then the default is zero.
[ February 16, 2006: Message edited by: marc weber ]
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this situation, it looks like only non-negative values are valid. So if you need some sort of "no data" flag, you could use a negative value. Just initialize the array (chromosome1) with...

Arrays.fill(chromosome1, -1);

(Note that you will need to import java.util.Arrays.)
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

A special null value is necessary to indicate that a reference points at nothing.

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.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or perhaps you shouldn't use an array at all, but a collection that grows when you put things into it?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic