I am a bit new to Java Programming. Two or three days ago, I encountered a Question regarding arrays in my mind which is give below.
Every Java Programmers knows, Array is a collection of Objects, it doesn't matter whether it contains primitive data types or Strings.
So my Question is, if Array is a collection of Objects so how does it treats or converts primitive data type into objects, because in Java, Primitive Data Type is different from Objects(like Strings). Consider the following program:-
int[] Array = new int[3];
Array[0] = 1;
Array[1] = 2;
Array[2] = 4;`
for(int a=0;a<Array.length;a++) System.out.println(Array[a]);
I made the Array or Array Object using new keyword and datatype follows it. This is, of course, workable for arrays. But when I do something like this for variable, it would get fail.
int var1 = new int 3;
For attention, asking again, how does in Java Array treats or converts the Primitive Data Type as Objects, since generally Primitive Data Types are not Objects.
Thank You!
in java, there are 2 categories of types: primitive and reference(i.e. objects)
An array type (whether it's a primitive array or an object array) is always a reference type. For exmaple, int[]
is a subtype of Object
. You can call any methods in Object
on an int[]
.
Nevertheless, int[]
is an array of primitives, not objects.