Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Reference Type  RSS feed

 
jnrohit Jain
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am reading book "Oracle Certified Associate Java SE 8 Programmer I Study Guide" and I came across with a problem when I was doing hands on practice of example given on page 25 related to "Reference Types".
I am using JDK 1.8.0_102 version. Below is the code and my question:-

Program 1:Program 1:


Result:
C:\>javac ReferenceType.java
C:\>java ReferenceType
Value of reference: null


Program 2:Program 2:


Result:
C:\>javac ReferenceType.java
C:\>java ReferenceType
Value of reference: null
Exception in thread "main" java.lang.NullPointerException
        at ReferenceType.main(ReferenceType.java:5)


Question: 1. Program 1 ran successfully. What value is stored in variable "reference"? String 'null' or Value null?
2. Why Program 2 is throwing runtime exception at line 5?

Thanks!
 
Knute Snortum
Sheriff
Posts: 4091
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The variable reference has the special value of nullprintln is smart enough to know this and print the String "null", but that does not mean that reference is a String.
 
Tobias Bachert
Ranch Hand
Posts: 85
18
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The null reference is stored in "reference".
String concatenation implicit performs String.valueOf, which itself does not invoke toString on null references but returns the char sequence "null" instead, thus program 1 runs successfully. In program 2 you try to invoke a method on the null reference, which leads to the NPE.
https://docs.oracle.com/javase/specs/jls/se8/html/jls-5.html#jls-5.1.11 wrote:Now only reference values need to be considered:
    If the reference is null, it is converted to the string "null" [...]
    Otherwise, the conversion is performed as if by an invocation of the toString method of the referenced object with no arguments; but if the result of invoking the toString method is null, then the string "null" is used instead. 
 
Dave Tolls
Rancher
Posts: 2914
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not println() doing that.
It's the String concatenation.
It uses a StringBuilder, so the null Object is added using the append() method, which calls String.valueOf(), which does a special null check.
 
Campbell Ritchie
Marshal
Posts: 55799
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I removed the line numbers from inside the code tags (thank you for using the tags) because line numbers are added automatically.
You can read about the behaviour of the + operator on Strings in the Java® Language Specification (=JLS). That contains a link to the same source that TB quoted. Beware: the JLS can be very difficult to read.is
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!