0

Which constructor is called when we try to do the follow:

int[] arr = new int[10];

From what I understand, for every single time that we use the keyword new, an object is created inside the Heap, using the constructor of the respective class, and its address is returned back to the reference variable.

My question is; Since Boolean, byte, short, character, int.... are all primitives and don't have a built in class or a constructor (at least I couldn't find one inside the src.zip),

boolean[] b = new boolean[10];
byte[] by = new byte[10];
short[] s = new short[10];
char[] ch = new char[10];
int[] i = new int[10];
long[] l = new long[10];
float[] f = new float[10];
double[] d = new double[10];

What object is returned when we try to create their array using new and how is that object created?

Zoe
  • 27,060
  • 21
  • 118
  • 148

1 Answers1

1

An array is an object in Java.

The values in an array of primitives are initialized to their respective default value (e.g. 0 for int, 0.0 for double, etc.)

The values in an array of Objects are initialized to point to null references until you assign them.

duffymo
  • 305,152
  • 44
  • 369
  • 561