• 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
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

The true meaning of null in Java or any programming language?

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is this "null" really and why can every type of Class reference variable be assigned null ?
Is null some kind of special object ? If not, then it must be a reference to something, call it a "hole".
What is this "hole" like ? Please tell me if you know what it is and/or suggest some books which clearly
explain things like these.

 
Master Rancher
Posts: 4353
59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Jason wrote:Is null some kind of special object ?


No. It's not an object at all.

David Jason wrote:If not, then it must be a reference to something


No. It's for a reference that doesn't reference anything.
 
David Jason
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Simmons wrote:

David Jason wrote:Is null some kind of special object ?


No. It's not an object at all.

David Jason wrote:If not, then it must be a reference to something


No. It's for a reference that doesn't reference anything.



What does it "look like" ? I am not well versed with inner workings of a computer.
So, its difficult for me to imagine what is happening.
eg. When a computer gets a Car reference, it tries to search an object which should have wheels,
engine, seats etc assuming that all Cars always have these things. When the comp encounters
Car carz = null; VS when it encounters a reference pointing to a real object ?
 
Mike Simmons
Master Rancher
Posts: 4353
59
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well internally, a reference is probably an address of some sort to locations in memory where objects are stored. A null is probably a default value for the address, quite possible 0 or maybe -1 or something like that, which the system knows cannot possible refer to a real object.

Imagine you've got a bunch of envelopes containing house addresses - each one is like a reference to the house at that address. Now assume that one of the envelopes is blank. No address. You look at it and can tell there's no address, and therefore no house that we can talk about. Now someone comes along and asks, "what does that house look like?" How would you answer such a question?
 
Marshal
Posts: 77152
370
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you look in Thinking in Java by Bruce Eckel, he suggests a null reference is address 0. I do not know whether the Java Language Specification is helpful on this point.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Jason wrote:Is null some kind of special object ? If not, then it must be a reference to something, call it a "hole".
What is this "hole" like ? Please tell me if you know what it is and/or suggest some books which clearly
explain things like these.


Interesting question. I think the others have covered most of it; but it's worth knowing that in some languages (NOT Java; but in Smalltalk, I seem to remember, and probably some others) Null (or Nil) is a first-class type, sitting alongside Object in the hierarchy; and very possibly implemented as a singleton.

This Wikipedia page may help to explain it a bit better than me. In it's terms, all Java reference types are "nullable".

It's might also interest you to know that v6 introduced a NullType into the Type hierarchy for parsing generic types.

Winston
 
Campbell Ritchie
Marshal
Posts: 77152
370
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That Wikipedia page looks rather incomplete. The Java Tutorials wasn’t particularly helpful about this question.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic