Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Enum Doubt

 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


here the answer is null, why isnt there a Null Pointer Exception.? :?:
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why it should throw nullpointerexception ?
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wont it call the toString() method, which enums override from the Object class?
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Days d is just a reference where is object assigned to it ? It has been assigned null, and for null JVM always prints null as output.
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As often is the case, the JVM has the answer.

If you look up the PrintStream (System.out is a PrintStream object) println(Object) method, you'll see that it calls print(String.valueOf(Object)) then println().
In the String API, String.valueOf(Object) then states "if the argument is null, then a string equal to "null"; otherwise, the value of obj.toString() is returned."
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
pete stein wrote:As often is the case, the JVM has the answer.

If you look up the PrintStream (System.out is a PrintStream object) println(Object) method, you'll see that it calls print(String.valueOf(Object)) then println().
In the String API, String.valueOf(Object) then states "if the argument is null, then a string equal to "null"; otherwise, the value of obj.toString() is returned."


You are talking about this na.


Really nice info Pete

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic