Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Wrappers

 
akash azal
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


why this code gives nullPointerException .
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
akash azal
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks
 
Carl Cordova
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
 
Massimo Battestini
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9510
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Leonard Fischer
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 874
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Leonard,

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


yes certainly its possible. This is good tutorial to start
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic