2

I have this method:

void stuff(Object[] array){
    // .. do stuff with the array
}

And trying to do this:

stuff(intArray);

It doesn't compile. I have to manually convert my int[] to an Integer[].

Isn't autoboxing supposed to take care of this stuff for me? Why doesn't it?

Also since turns out the Java language doesn't do this natively, why isn't there a utility in the JDK to do this stuff? Isn't this why we have a standard library?

Aviv Cohn
  • 15,543
  • 25
  • 68
  • 131

1 Answers1

3

autoboxing doesn't work for array, autoboxing uses static valueOf() method of wrapper class to convert primitive to its wrapper Object

Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes. For example, converting an int to an Integer, a double to a Double, and so on. If the conversion goes the other way, this is called unboxing.

jmj
  • 237,923
  • 42
  • 401
  • 438