• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

null = 0?

 
Travis Benning
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I've hit a question that is on two mocks:
On Jxam:
System.out.println(Integer.parseInt(null));
answer = 0
On another mock (forgot name):
System.out.println(Integer.parseInt(null));
answer = error

Now I initially thought it was 0, but I took the second one first and got error, so I remembered this, and then on Jxam I get it wrong again.
Can someone please clarify this please?
All help appreciated,
Travis B.
 
Rowan Chattaway
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Travis, this is what happens when I tried to run it:
Exception in thread "main" java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at Parser.main(Parser.java:5)
so, I guess the answer is error!?
 
Travis Benning
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Thanks Rowan.
and a big DUH!! for me. lol
After all my studying overnight and everything.... the simplest thing would have been to test it myself, but I missed it. lol.
This must mean I need sleep.
Thanks for your help Rowan.
Travis B.
 
Rajinder Yadav
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Travis here is one way to think about it (in java).
A null is an undefined value, while zero in a defined value. So trying to display an 'null' value could have one of two results:
a) it outputs the literal 'null'
b) it results in an runtime error
there is no easy way to guess if the correct answer is a) or b) without actually trying it out with java and seeing how the creators of the language wanted it to (mis)behave
[ March 06, 2002: Message edited by: Rajinder Yadav ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic