Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
David Jason
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • 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.

 
Mike Simmons
Ranch Hand
Posts: 3028
10
  • Mark post as helpful
  • send pies
  • 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
  • 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
Ranch Hand
Posts: 3028
10
  • Likes 2
  • Mark post as helpful
  • send pies
  • 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?
 
Campbell Ritchie
Sheriff
Posts: 48363
56
  • Likes 2
  • Mark post as helpful
  • send pies
  • 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.
 
Winston Gutkowski
Bartender
Pie
Posts: 10084
55
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 48363
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That Wikipedia page looks rather incomplete. The Java Tutorials wasn’t particularly helpful about this question.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic