• 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

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

 
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)



 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.


 
Ranch Hand
Posts: 257
Hibernate Oracle Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Salil Vverma
Ranch Hand
Posts: 257
Hibernate Oracle Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.



 
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Atwal Usha
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, it will be runtime exception and not compiler error.
 
reply
    Bookmark Topic Watch Topic
  • New Topic