• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Wrapper, boxing == huh????

 
Richard Boren
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I�ve read, reread and studied the part covering Wrappers, autoboxing, and !=, == and equals() in the K&B book, the API for the wrappers and the Lang Specification until my eyes are bleeding. I do not understand the following cases�

A) �!=�
Integer a = new Integer( 5 ) ;
Integer b = new Integer( 5 );

B) �!=�
Integer a1 = 7 ;
Integer b1 = new Integer( 7 );

C) �==�
Integer a2 = 9 ;
Integer b2 = 9 ;

Why are A and B �!=� ? I thought �Integer x = 1;� was equivalent to �Integer x = new Integer(1);�.

Here is the code and its output:


I have tried Boolean and Byte with the same results. What am I missing

Richard
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The key here is that anytime you use the new operator, you create a new object.

So in the case of



a and b refer to 2 different objects.

Autoboxing is guaranteed to autobox the same primitive int to the same Integer object provided the int is between -128 and 127.

However, if you look at the second case, you are still creating a new object.



b1 refers to a new Integer object. a1 refers to an Integer object created through autoboxing.

In the third case, you are autoboxing the same int within the guaranteed range into the same Integer object.



So a2 and b2 refer to the same Integer object.
 
Richard Boren
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I�ve got it.

I see this autoboxing behavior holds true with methods that have wrapper type parameters.

Thanks Keith.

Richard
 
Soma Kora
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Im studying K&B book, I didn't undertstand code example given in chapater3:

class Boxing2{
static Integer x;
public static void main(String[] arg){
doStuff(x);
}
static void doStuff(int z){
int z2=5;
System.out.println(z2+z);
}
}
Mentioned in book that, it complies fine and throws Run time exception and reason being mentioned as " becuase x doesn't refer to an Integer object there is no value to unbox."

doubt: In the above code doStuff(x), here x is an Integer object since it is a static class variable it has got default value 0. Why is this Runtime exception as mentioned in the book?

Any pointers greatly appreciated.

Thanks
Som.
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, it doesn't have 0 by default.

Since the type of x is Integer, by default it is assigned null.
 
Soma Kora
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, right. Thanks.
 
Pankaj Patel
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Keith.

Autoboxing is guaranteed to autobox the same primitive int to the same Integer object provided the int is between -128 and 127.

I had checked this sentence, and this is perfectly right. But one doubt in my mind is, the size of int is 4 byte so it can accomodate larger int size then the given -128 to 127. So what could be the reason?

Can you explain me. Thanks.
 
Richard Boren
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Autoboxing is guaranteed to autobox the same primitive int to the same Integer object provided the int is between -128 and 127.


In K&B 1.5 on p239 under the Boxing, ==, and Equal() section covering "==" and autoboxing it says "... In order to save memory..."

Richard
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Pankaj Patel:
Hi Keith.

Autoboxing is guaranteed to autobox the same primitive int to the same Integer object provided the int is between -128 and 127.

I had checked this sentence, and this is perfectly right. But one doubt in my mind is, the size of int is 4 byte so it can accomodate larger int size then the given -128 to 127. So what could be the reason?

Can you explain me. Thanks.


This is the discussion from the Java Language Specification.

Discussion

Ideally, boxing a given primitive value p, would always yield an identical reference. In practice, this may not be feasible using existing implementation techniques. The rules above are a pragmatic compromise. The final clause above requires that certain common values always be boxed into indistinguishable objects. The implementation may cache these, lazily or eagerly.

For other values, this formulation disallows any assumptions about the identity of the boxed values on the programmer's part. This would allow (but not require) sharing of some or all of these references.

This ensures that in most common cases, the behavior will be the desired one, without imposing an undue performance penalty, especially on small devices. Less memory-limited implementations might, for example, cache all characters and shorts, as well as integers and longs in the range of -32K - +32K.



5.1.7 Boxing Conversion
 
Pankaj Patel
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. I got the reason.

Thanks you very much, everybody.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic