0

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.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Pål Hart
  • 19
  • 6
  • 5
    Arrays of primitive types are not primitive types, and therefore need no auto boxing, period. – Sweeper Jan 11 '23 at 01:29
  • I've never heard of autoboxing arrays. Only primitives are autoboxed and autounboxed. Oracle doesn't have an `CharArray`, but some workplaces, schools, or others might have created some. – Old Dog Programmer Jan 11 '23 at 01:30
  • @Sweeper Are you saying that in Java the types `int[]`, `char[]`, et cetera, do not function like the fundamental array's of other languages (as a pointer to the first in a sequential chunk in Memory of the associated types)? I've never heard someone say that they are Object's before. If they are, what Classes are they associated with? That would be very helpful for me to know. – Pål Hart Jan 11 '23 at 02:00
  • @OldDogProgrammer I thought "array of primitives" could be confused with the Array Class's that use primitives (e.g. `CharArray` and `IntegerArray`). Oracle doesn't have them as you've said, but I've never worked with an IDE that didn't support them. They're at least common enough that a significant number of people would assume that they are what is meant by "array of primitives." Where as I was referring to e.g. `char[]` and `int[]`. – Pål Hart Jan 11 '23 at 02:03
  • 1
    In Java, any array, even an array of primitives, is a reference type. Like other reference types, they inherit methods from [Object](https://docs.oracle.com/javase/10/docs/api/java/lang/Object.html). For example, if you had `int [] frobby = new int [30];`, you could have `frobby.notifyAll();` An array has a `length` attribute. – Old Dog Programmer Jan 11 '23 at 02:07
  • @OldDogProgrammer Thank you for your explanation of Java arrays. I don't think anyone quite explained that detail to me before. I suppose this means the BCPL style ones don't exist in Java. I almost never use them in Java, so this hadn't come up before. – Pål Hart Jan 11 '23 at 02:43
  • @OldDogProgrammer A Java professor I once had said that the only Pointers in Java were managed Pointers (E.G. the Smart Pointers in C++). Combine that with all the times I've heard that Java is Pass-By-Value only, and the use of the `new` keyword, and this whole time I've been under the impression that these were Pointers. I thought that reference types didn't exist in Java. This will change how I think of coding in Java. – Pål Hart Jan 11 '23 at 02:53
  • There is actually no reason to have wrapper classes for arrays of primitives in Java. – Old Dog Programmer Jan 12 '23 at 02:57

1 Answers1

2

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.

  • Thank you. The section on autoboxing was informative. As this guide and commenters pointed out, I was mistaken regarding the existence of primitive arrays in the Java language. It does still leave me wondering about the nature of the Classes which are deceptively implemented in that style though. (Id est *how* the Class of say `char[]` would be written in Java.) – Pål Hart Jan 11 '23 at 19:22