What's the difference between boxing and unboxing?
What's the difference between boxing and unboxing, I'm a bit confused?
Is autoboxing the same as boxing?
This is what I know so far:
-This is boxing, i think:
int []arrayset ={1,2,3};
but I don't understand what unboxing is, can someone explain it to me.
[380 byte] By [
vopoa] at [2007-11-27 9:59:43]

No what you have there is an example of putting some ints into an array. There's no noun/verb/gerund for this: "arrayging"?
Autoboxing is described here: http://java.sun.com/j2se/1.5.0/docs/guide/language/autoboxing.html
To box a primitive to create the corresponding object wrapper. For instance to go from the primitive int 42 to the object new Integer(42). Unboxing is the reverse process: going from an Integer to an int.
Some time back Java introduced the idea of autoboxing - where you can use wrapper classes and primitives interchangably in many places.Integer j = 1 + new Integer(41);
The link gives less silly examples and discusses them.
> I did little research, but please correct me if I'm
> wrong:
>
> Is this similiar to unboxing:
> > System.out.println(intArray[0] + intArray[1] +
> intArray[2]);
>
Only if intArray is declared asInteger[] intArray ...
Then the Integers at intArray[0] etc are unboxed into int primitives before adding.
dwga at 2007-7-13 0:30:47 >

> > I did little research, but please correct me if
> I'm
> > wrong:
> >
> > Is this similiar to unboxing:
> > > > System.out.println(intArray[0] + intArray[1] +
> > intArray[2]);
> >
>
> Only if intArray is declared asInteger[]
> intArray ...
> Then the Integers at intArray[0] etc are unboxed into
> int primitives before adding.
Did you try that before you posted it? Autoboxing is applied only to an individual primitive/wrapper. Arrays and collections aren't subject to autoboxing/unboxing. Basically, the compiler will fill in constructs such as Integer.valueOf() for you, it doesn't go as far as generating loops and new arrays for you
OP, as Peter said, the difference between boxing and unboxing is merely one of direction. Autoboxing wraps a primitive up for you, auto-unboxing extracts the primitive from a wrapper
> > > I did little research, but please correct me if
> > I'm
> > > wrong:
> > >
> > > Is this similiar to unboxing:
> > > > > > System.out.println(intArray[0] + intArray[1] +
> > > intArray[2]);
> > >
> >
> > Only if intArray is declared asInteger[]
> > intArray ...
> > Then the Integers at intArray[0] etc are unboxed
> into
> > int primitives before adding.
>
>
> Did you try that before you posted it? Autoboxing is
> applied only to an individual primitive/wrapper.
> Arrays and collections aren't subject to
> autoboxing/unboxing. Basically, the compiler will
> fill in constructs such as Integer.valueOf() for you,
> it doesn't go as far as generating loops and new
> arrays for you
>
> OP, as Peter said, the difference between boxing and
> unboxing is merely one of direction. Autoboxing wraps
> a primitive up for you, auto-unboxing extracts the
> primitive from a wrapper
Am fully aware of that, the OP's example contained references to array elements but it was not clear whether that array was declared as Integer[] or int[], hence my answer.
And just for the recordInteger[] intArray = new Integer[] {1, 2, 3}; // autoboxing
System.out.println(intArray[1] + intArray[2]); // autounboxing
// while ...
int[] intArray = new int[] {1, 2, 3}; // no boxing
System.out.println(intArray[1] + intArray[2]); // no unboxing
is what i meant.
dwga at 2007-7-13 0:30:47 >
