aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Uninitialized local reference variables Vs local referenced variables assigned null Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Uninitialized local reference variables Vs local referenced variables assigned null" Watch "Uninitialized local reference variables Vs local referenced variables assigned null" New topic
Author

Uninitialized local reference variables Vs local referenced variables assigned null

Andy James
Ranch Hand

Joined: Aug 22, 2005
Posts: 90
Why does the Java compiler and Runtime environment treat the local variables s1 and s2 differently? None of s1 and s2 is assigned a String object.



Andy
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4492
    
    8

You haven't initialised s1, but you have initialised s2 (to null, but that doesn't matter - it's still a value). Local variables are not automatically initialised to anything, and the compiler will make sure you do before letting you use them.

Setting it to null will sometimes result in a NullPointerException at run-time (not at compile-time), but the println method is written to be able to cope with null arguments so that doesn't happen.
Andy James
Ranch Hand

Joined: Aug 22, 2005
Posts: 90
Thanks for your reply, Matthew. I understand what you have written. What I fail to understand is - how does it affect the java compiler. Isn't assigning null as good as not assigning a value. How does it make a difference to the compiler? The compiler doesn't get a value for any of the variables - s1 and s2.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3742
    
  16
Andy James wrote:The compiler doesn't get a value for any of the variables - s1 and s2.

Yes it does. s2 has the value null. It is a valid value that can be used (for example) in comparisons

You couldn't do this for s1.


Joanne
Andy James
Ranch Hand

Joined: Aug 22, 2005
Posts: 90
Okay. So, I get that null is a value.

What happens when multiple object references are assigned null? Does null has an address? Cause we know that reference variables store addresses of the objects that they refer to.



Thanks.

Andy
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3742
    
  16
Andy James wrote:Does null has an address? Cause we know that reference variables store addresses of the objects that they refer to.

Reference variables store a value that the JVM can use to access the object referred to. It may be an address, but doesn't have to be. null is a special value that the JVM recognises as signifying that no object is being referred to. You will have to read the Java Language Specification to find out if null is represented by a specific value, but I suspect it is up to the designer of the JVM.
Andy James
Ranch Hand

Joined: Aug 22, 2005
Posts: 90
Hi Joanne,

Thanks very much for your reply. I digged into 'the Java Language Specification' and 'The Java Virtual Machine Specification' and found the following interesting information:

Java Language Machine Specification

2.4 Reference Types and Values

A reference value may also be the special null reference, a reference to no object, which will be denoted here by null. The null reference initially has no run-time type, but may be cast to any type. The default value of a reference type is null.

The Java Virtual Machine specification does not mandate a concrete value
encoding null.

Similarly, while null might appear to be a keyword, it is technically the null
literal (§3.10.7).

A literal is the source code representation of a value of a primitive type (§4.2), the String type (§4.3.3), or the null type (§4.1).

3.10.7 The Null Literal
The null type has one value, the null reference, represented by the null literal null, which is formed from ASCII characters.
NullLiteral:
null
A null literal is always of the null type (§4.1).


4.1
In practice, the programmer can ignore the null type and just pretend that null is merely a special literal that can be of any reference type.



4.7.4 The StackMapTable Attribute
• The Null_variable_info type indicates that location contains the verification type null.
Null_variable_info {
u1 tag = ITEM_Null; /* 5 */
}


So it seems that its upto a particular JVM implementation to decide what value to use to denote a null literal. Thanks again!

Andy
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4492
    
    8

Andy James wrote:So it seems that its upto a particular JVM implementation to decide what value to use to denote a null literal.

Exactly. In lower-level languages like C, null is the direct equivalent of the address 0. But Java works at a slightly higher level of abstraction - null might be represented as 0 internally but as long as it has the effect defined in the JLS it doesn't matter how it's done.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Uninitialized local reference variables Vs local referenced variables assigned null