• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Reference Type

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The variable reference has the special value of null.  println is smart enough to know this and print the String "null", but that does not mean that reference is a String.
 
Ranch Hand
Posts: 86
18
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.  

 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic