aspose file tools*
The moose likes Beginning Java and the fly likes Null Literals Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Null Literals" Watch "Null Literals" New topic
Author

Null Literals

Honey
Greenhorn

Joined: Mar 02, 2000
Posts: 7
Could you please tell me the Difference between Null Literals and Null (the Keywords)?


greenhorn
Tony Alicea
Desperado
Sheriff

Joined: Jan 30, 2000
Posts: 3222
    
    5
I'm moving this thread to the Beginner section...


Tony Alicea
Senior Java Web Application Developer, SCPJ2, SCWCD
paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20629
    ∞

I would guess it has to do with the value that is stored in a reference when it is null vs. the the keyword null that you would compare it to or set it to.
The value null vs. the keyword null.


permaculture Wood Burning Stoves 2.0 - 4-DVD set
Dave Brookes
Greenhorn

Joined: Feb 23, 2000
Posts: 17
null means nothing
whereas "" means the empty string

------------------
Regards,
Dave


Regards,<br />Dave
Honey
Greenhorn

Joined: Mar 02, 2000
Posts: 7
I would say that Null is a keyword whereas Null literal is not a keyword but Null literal could have only Null as value.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Urg. "null literal" and "null keyword" sound to me like two names for the same thing - except that "null keyword" isn't really correct since null isn't really a keyword at all, but it is a reserved word which is what people often really mean when they talk about keywords. I'm not going to try to make sense of everyone's statements, since everyone seems to have a different interpretation, but I do have two more important points:
  1. The word "Null" has no special meaning at all in Java - "null" does. Capitalization is important in this language.
  2. The empty string "" should not be called a null at all in Java, as that just creates confusion. An empty string is an actual String object with no characters in it and length 0. A null is not an object at all; it represents the lack of any object.


"I'm not back." - Bill Harding, Twister
Theresa Duick
Greenhorn

Joined: Feb 16, 2000
Posts: 27
Please explain null values. If there are varibles such as int i or j or an int array without specifically assigned values, are the values then considered null? On the other hand, if there is an empty string array, is it just empty or is it null? I am confused about this.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
When u declare a variable, you have to initialize it, unless u specify that variable as static, otherwise compiler will complain, that the variable may not be initialized. In case of arrays, when you create a int array, all the elements are initialized by default to 0 and the String array is initialized to "null"( which is nothing).
Hope this helps.
[This message has been edited by Java2learner (edited March 02, 2000).]
Theresa Duick
Greenhorn

Joined: Feb 16, 2000
Posts: 27
Thanks! Key lesson to remember is: array elements not initialized are set to zero & strings set to null. Hopefully I won't forget this in a few days!
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Errr... not quite. When an array is created, any elements not initialized will be set to the appropriate default value for the type of the array. If it's an array of primitive numerics (byte, char, short, int, long, float, double) then the default value is 0. If it's boolean, the default value is false, and if it's any kind of reference type (any sort of Object, including String) then the default value is null.
As for Java2Learner's statement about "when you declare a variable, you have to initialize it, unless it's static" - that's incorrect. If it's a local variable then you have to initialize it (and local variables can't be static anyway). If it's an instance or class variable (i.e. a member variable with or without "static" modifier) then you don't actually have to initialize it - if you don't, it will get the appropriate default value for its type, as I listed above (0, false, or null).
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Jim, thanks for correcting my statement.
Honey
Greenhorn

Joined: Mar 02, 2000
Posts: 7
Thanks Jim. I really forgot about capitalization.


[This message has been edited by Honey (edited March 03, 2000).]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
According to JLS:
null is NOT A KEYWORD. It is a NULL LITERAL.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
No ,null is s keyword ,you can refer to Robert and Heller's book.
Or you can refer Sun Site.


Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
No ,null is a keyword ,you can refer to Robert and Heller's book.
Or you can refer Sun Site.


Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
OK, technically, in the real world, according to the Java Language Specification, "null" and "true" and "false" are not keywords but they are reserved words. Roberts/Heller/Ernest do not outrank the Java Language Specification.
However, in the exam,, which is what RHE are wrinting for, we don't care about this distiction. All Sun wants to know is, do you know that these words are illegal to use as identifiers? Hopefully, they will correctly call these "reserved words". If they slip and say "keywords" or "reserved keywords", just pretend they said "reserved words".
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hello Honey,
Do you know what MVSR means?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Null Literals