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.
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.
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.
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
Similarly, while null might appear to be a keyword, it is technically the null
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.
A null literal is always of the null type (§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.
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 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.