File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes while NULL? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "while NULL?" Watch "while NULL?" New topic
Author

while NULL?

Sam Bluesman
Ranch Hand

Joined: Nov 21, 2004
Posts: 191
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


Moosey knows best
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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 ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
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

Joined: Jul 15, 2003
Posts: 1078
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

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Sep 24, 2003
Posts: 1608

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.


Tony Morris
Java Q&A (FAQ, Trivia)
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
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

Joined: Jul 11, 2001
Posts: 14112
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
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: while NULL?