permaculture playing cards*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Chap 3 ,page 237 K&B SCJP5 book -Question on Wrappers from Exam Watch Section Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Chap 3 ,page 237 K&B SCJP5 book -Question on Wrappers from Exam Watch Section " Watch "Chap 3 ,page 237 K&B SCJP5 book -Question on Wrappers from Exam Watch Section " New topic
Author

Chap 3 ,page 237 K&B SCJP5 book -Question on Wrappers from Exam Watch Section

Lucky J Verma
Ranch Hand

Joined: Apr 11, 2007
Posts: 278
Hi

I have a doubt in an example given in exam-watch section on wrapper classes.
It has a Wrapper class object passed to to method that accepts PDT equivalent and what happens if Wrapper object is null.
It would throw NullPointerException.

But i dont understand how it works.
why the exception is thrown if its void go(int x) here and not in case of - void go(Integer x)



Atwal Usha
Ranch Hand

Joined: Sep 10, 2009
Posts: 137
Hi,

Here you get compiler error when you use PDT because in that case the value of an Integer reference variable(null) is being assigned to an int variable. Had that value being any number it would have not given any compiler error however in this case as the Integer reference variable has not been assigned any value it will get a default value of null and when this value is assigned to int variable it would result in compiler error due to incompatible types.

The compiler would expect an int type however it will find <nulltype> and thus compiler error.



Java Certification Exam Mock Tests: SCJA  SCJP 5  SCJP 6  SCJP 6 (Online Training)  SCJP 6 (Instructor Led Training)   SCWCD 5   SCBCD 5  SCEA 5
Salil Vverma
Ranch Hand

Joined: Sep 06, 2009
Posts: 253

Hey Atwal ,

The compiler would expect an int type however it will find <nulltype> and thus compiler error.


This code does not give any compilation error but throws an exception at run time. It is interpreter who sees that the value of variable x is null at run time and throws the exception.

Regards
Salil Verma


Regards
Salil Verma
Salil Vverma
Ranch Hand

Joined: Sep 06, 2009
Posts: 253

Hey Lucky,

Unboxing is a funda in java in which compiler identifies that when the programmer is trying to assign a wrapper to premitive type, it calls the corresponding function to return the premitive type. (As Integer.intValue() in case of integer)

The java compiler converts your code to the code as mentioned below.



In this function, at runtime the interpreter tries to call x.intValue(). As we know that the value of x is null. so It returns null pointer exception.
I hope this would clear your confusion.



Siva Masilamani
Ranch Hand

Joined: Sep 19, 2008
Posts: 385
You get NullPointerException because java try to conver the Integer to int.So it uses a intValue method implicitly.

In this case x is not pointing to any object and you know invoking any method wilth null reference will throw NullPointerException.

But if you use the wrapper class as an argument there is no need to boxing or unboxig and it will just display whatever the x points to(in this case prints null).

Hope this would help u.


SCJP 6,SCWCD 5,SCBCD 5

Failure is not an option.
Atwal Usha
Ranch Hand

Joined: Sep 10, 2009
Posts: 137
Sorry, it will be runtime exception and not compiler error.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Chap 3 ,page 237 K&B SCJP5 book -Question on Wrappers from Exam Watch Section
 
Similar Threads
Query on Wrapper
unboxing issues
JDK 1.5 AutoBoxing Confusion in Code
A question about passed by value
Wrapper throws NullPointerException, how to correct code