File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes null = 0? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "null = 0?" Watch "null = 0?" New topic
Author

null = 0?

Travis Benning
Ranch Hand

Joined: Jan 24, 2002
Posts: 74
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.


Sun Certified Programmer for Java 2 Platform
Rowan Chattaway
Ranch Hand

Joined: Jan 18, 2002
Posts: 75
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!?


The early bird may get the worm, but the second mouse gets the cheese.........<br /> <br />Sun Certified Programmer for Java 2 Platform<br />Sun Certified Web-Component Developer for J2EE Platform
Travis Benning
Ranch Hand

Joined: Jan 24, 2002
Posts: 74
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

Joined: Jan 18, 2002
Posts: 178
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 ]

<a href="http://www.rajindery.com" target="_blank" rel="nofollow">Rajinder Yadav</a><p>Each problem that I solved became a rule which served afterwards to solve other problems. --Rene Descartes
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: null = 0?