• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

while NULL?

 
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.)
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Liar, liar, pants on fire! refreshing plug:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic