Meaningless Drivel is fun!*
The moose likes Java in General and the fly likes null Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "null" Watch "null" New topic
Author

null

Tan Richards
Greenhorn

Joined: Sep 29, 2005
Posts: 18
Hi,
Can anyone please tell me what is actually the meaning of the keyword null in Java? What does it mean? Some indeterminate value?


"Failures are pillars of success" - That doesn't mean you go on building pillars
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Not an indeterminate value: one specific value that means "no object".


[Jess in Action][AskingGoodQuestions]
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Not an indeterminate value: one specific value that means "no object".


For example, you can reliably test for it:


There is no emoticon for what I am feeling!
Adam Richards
Ranch Hand

Joined: Nov 03, 2005
Posts: 135
Another way of thinking of it is to remember that variables are really "references" to actual objects (in other languages, these are sometimes called "pointers"). A variable exists as soon as you declare it, but doesn't "refer" to anything yet. It's at that point that we say it has a "null" value. Some people would say that its value at this point is "undefined."

If that's not enough to make your head spin, I don't know what is!
jiju ka
Ranch Hand

Joined: Oct 12, 2004
Posts: 306
To know the word 'null' you need to know the different data types.

The types of the Java language are divided into two categories: primitive types and reference types. Primitive data types are int, boolean, float etc. Non-primitives(reference) are object data types(an array is an object).

A value of null is possible only to variables refrenced by non_primitive types.

When you do a primitive variable declaration like

a memmory location is allotted for variable i. At the time of memmory allocation the data in this memmory location need to be wiped out. In java we store newly allotted memmory location with default value. The default value changes with data type.

For int it is zero. So zero gets filled in.

When you do a non-primitive variable declaration like

a memmory location is allotted for variable iw. For non-primitive types (all refrence types)default value is null. So null gets filled in.


Now comes the question what is being copied as part of null. Is there a unicode corresponds to null?


In java 'null' just means that the 'non-primitive variable's content is missing'. Assuming there is only one way to represent emptiness, the question really is

Is there a unicode for emptiness?

unicode represents a char (or value in a variable of Character data type).



And the answer is no. You may check it here
http://www.unicode.org/charts/charindex.html
It is not combination of unicodes either.


Since null is not a character or combination of characters, how does jvm distinguishes between the emptiness and non-emptiness.


So there is a third type of data which is not named
java specfication at http://java.sun.com/docs/books/vmspec/html/Concepts.doc.html#22930 says..


The types of the Java language are divided into two categories: primitive types and reference types. There is also a special null type, the type of the expression null, which has no name. The null reference is the only possible value of an expression of null type, and can always be converted to any reference type. In practice, the Java programmer can ignore the null type and just pretend that null is a special literal that can be of any reference type.


So the jvm uses a data type of special null type to distinguish between the emptiness and non-emptiness.

The reason many programmers take null as a literal is evident above.
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1817

A variable exists as soon as you declare it, but doesn't "refer" to anything yet. It's at that point that we say it has a "null" value. Some people would say that its value at this point is "undefined."


Not entirely true. "undefined" and "null" are separate!



In line 11, we define aConnection but don't assign it a value. Theoretically, this reference could refer to anything. (Java won't let you do anything with these; the compiler will report an error if you try to use a variable to which you have not assigned a value.) (In C/C++, in line 11 the aConnection variable points to a random place in memory.)

In line 12, we explicitly set aConnection to reference a null object. We can now use the variable, although attempting to call methods on it will result in a NullPointerException. (In C/C++, we would be setting the value to point to memory location 0x00000000; attempting to call a method on it would result in a segmentation fault.)

In line 13, we assign it a (presumably) non-null value and can use it normally. (Although it is conceivable that getConnection() could return null....)


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Grant Gainey
Ranch Hand

Joined: Oct 16, 2005
Posts: 65
Think of a reference as if it were your mailbox.

Referring to the previous example - at line 13, the mailbox contains a letter to you.

At line 12, it contains a note from the mailman saying you got no mail today.

At line 11, it's Schroedinger's Mailbox. You don't know what's in it, and Java won't even let you open it. If you have a letter in your hand, or a note (null), you can stuff it through the slot - and then Java will let you open it.

Some languages let you "open" a Schroedinger's Mailbox like line 11. You get whatever random crud was left there by the last user of that space.

Does that help any?

Grant


In Theory, there is no difference between theory and practice.<br />In Practice, there is no relationship between theory and practice.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
Some languages let you "open" a Schroedinger's Mailbox like line 11. You get whatever random crud was left there by the last user of that space.

Java designers had that security gap in mind - thats why ALL memory allocations are filled with zeros before use. Reference variables interpret this as "null", numerics as zero, etc etc.
Bill
Grant Gainey
Ranch Hand

Joined: Oct 16, 2005
Posts: 65
Originally posted by William Brogden:

Java designers had that security gap in mind - thats why ALL memory allocations are filled with zeros before use. Reference variables interpret this as "null", numerics as zero, etc etc.
Bill

Not local variables. This is why you get the "local variable may not have been initialized" compilation error - you have to explicitly assign something to it before use, unlike member variables which are initialized for you.

Grant
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
It's an unfortunate language defect that attempts to resolve the issue of 'definite assignment' of fields. That is, there are (many in practice) circumstances where the compiler cannot resolve whether or not a field has been definitely assigned. To give a trivial example:

The compiler cannot determine if m1 or m2 were called first, which then means, it doesn't not know if at a call to m2, 'o' has been assigned. Of course, if the Java language designers gave you the ability, you could 'annotate' it in some way that mandates that m1 is called before m2, guaranteeing assignment, but then you'd have a procedural language i.e. the concept violates the very single most important fundamental of OO (the rest are loosely defined on intuition and are mostly fallacy).

Instead, the language should have failed immediately at runtime in cases where:
a) the compiler could not determine whether a field is definitely assigned (we all know that a local is simple to determine right?)
b) the field is used in any way, such as dereferencing, a method argument.

Perhaps a language like this will exist some day but until then, you'll have to put up with the defective existance of the 'null' concept. The optimal workaround is to declare and indeed throw a NullPointerException for any method that accepts one or more reference types. Welcome to Java

Fail early; it is a type-safe language after all.

Edit:
Fixed minor typo.
Clarified point b)
[ November 05, 2005: Message edited by: Tony Morris ]

Tony Morris
Java Q&A (FAQ, Trivia)
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: null