wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Wrappers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Wrappers" Watch "Wrappers" New topic
Author

Wrappers

akash azal
Ranch Hand

Joined: Jan 31, 2009
Posts: 70


why this code gives nullPointerException .


We will keep things moving!!
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

The Integer you pass to doStuff is being boxed out (i.e converted into an int). To do so, Integer.intValue will be called on your variable, but because it is not initialized (it's null here), an NPE is thrown.

This would also happen if you'd call "int z = x;" without x being initialized.


[My Blog]
All roads lead to JavaRanch
akash azal
Ranch Hand

Joined: Jan 31, 2009
Posts: 70
thanks
Carl Cordova
Greenhorn

Joined: Feb 02, 2008
Posts: 4
I attempted to take this code a step further by overloading the doStuff method and initializing the static wrapper. I was expecting the end value of x to be 10, but it is actually 5. I don't understand what is happening.

Here is the output:

$ java Box6
Value of i in doStuff(Integer) is : 10
Value after doStuff is : 5

Thanks,

Carl Cordova
Massimo Battestini
Greenhorn

Joined: Nov 21, 2008
Posts: 11
What I believe is happening is that in the operation:

i = i + y;

it is unboxing i, then it adds y and finally re-box everything and assign it to i. But while re-boxing it creates a new object;

in this way i inside doStuff and i inside main will have a reference to two distinct objects.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

Well wrappers are immutable, so if you reassign a value to them, then a new object is created.



And this is just one part of the story. Since you are using the wrapper in a method call, so it will be passed as a reference pointer (this is what I call it). If it was a primitive, then there was no chance of change in the value of original variable as primitives are passed as value. But even if we pass a reference to the method, the original value of x will not change as in doStuff, a new object will be created o point to the value 10 and x will still point to the Integer object with the value 5. I hope it is clear...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Leonard Fischer
Ranch Hand

Joined: Apr 17, 2009
Posts: 47
I appreciate Ankit's most interesting observation about about Integer instances being immutable. I'd like to request some moreinfo about this, because I'm concerned about how to recognize when an object is immutable.

In the following code,

static Integer fn1(Integer i)
{
i = i + 1;
return i;
}
static Animal fn2(Animal a)
{
a.name = "New name";
Animal b = a;
return b;
}

Consistent with what Ankit has said, fn1() does not change the value of the ARGUMENT i, since Integer is immutable. fn2() does change the value of the ARGUMENT a; the "name" field is "New name".

I would be most curious if anyone knows which classes are immutable, and which are not. Of course, String is immutable. I believe that Date is immutable. Can anyone provide a list of immutable classes? Is there a way to define your own class, and make it immutable?
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874



Leonard,

Is there a way to define your own class, and make it immutable?


yes certainly its possible. This is good tutorial to start
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Wrappers