Home > Enterprise >  When autoboxing, does a primitve type array autobox to an array of the Wrapper class for the primitv
When autoboxing, does a primitve type array autobox to an array of the Wrapper class for the primitv

Time:01-12

Although I've taken 2 university classes in Java in the past, I don't recall this. I've researched this online, but could only find references to the associated class type for each primitive type, and no specific indication of what happens to primitive type arrays.

Primitive Type  Wrapper Class
boolean         Boolean
byte            Byte
char            Character
short           Short
int             Integer
long            Long
float           Float
double          Double
boolean[]       ?
byte[]          ?
char[]          ?
short[]         ?
int[]           ?
long[]          ?
float[]         ?
double[]        ?

The only reason I can think of for it to not be mentioned at all is if there is no difference: for example, char[] becomes Character[]. However, if I recall correctly there are array wrappers (e.g. CharArray; although I can't remember if those are implementation defined or not). However the thing that would make the most sense for autoboxing a char[] would be String, like it is in other languages.

So, for example, if the method took Object would char[] become Character[], CharArray, or String? Can it depend on the circumstances (with overloaded methods)? For example, with a method taking an Object it becomes CharArray in that one, but if there's an overload for String it uses that one instead.

It was mentioned in the comments that the arrays for primitive types (e.g. CharArray or IntegerArray) are not provided by Oracle. Thus I wonder if there is a priority system on autoboxing that is implementation defined, or if perhaps the autoboxing of primitive types and primitive type arrays to be used with a method taking Object is always the same.

CodePudding user response:

During my studies Econometrics at the Erasmus University, as a teacher assistant I wrote this Java programming guide together with my programming teacher. It states that there are 8 primitive variables, namely the eight ones on top of your list. Arrays are not primitive and can therefore not be autoboxed.

Then, this paragraph explains everything you need to know about autoboxing.

  • Related