aspose file tools*
The moose likes Beginning Java and the fly likes The true meaning of null in Java or any programming language? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "The true meaning of null in Java or any programming language?" Watch "The true meaning of null in Java or any programming language?" New topic
Author

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

David Jason
Greenhorn

Joined: Aug 21, 2012
Posts: 27
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.

Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
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

Joined: Aug 21, 2012
Posts: 27
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
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
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?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39828
    
  28
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.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8207
    
  23

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39828
    
  28
That Wikipedia page looks rather incomplete. The Java Tutorials wasn’t particularly helpful about this question.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: The true meaning of null in Java or any programming language?