Suhail Gupta
Suhail Gupta

Reputation: 23276

How are these types differentiated?

I read here @ java.sun that int[] iarr is a primitive array and int[][] arr2 is a object array. What is the difference between a primitive type and an object type ? How are the above two different from each other ?

Upvotes: 1

Views: 53

Answers (2)

AlexR
AlexR

Reputation: 115378

int[] is a primitive array because it contains elements of primitive type int. Every array itself is Object, so array of primitives is object too.

int[][] is a array of int[], i.e. each element of int[][] contains array of integers. But since array is a object int[][] contains objects, not integers.

Upvotes: 4

vahidg
vahidg

Reputation: 3963

From the link you've provided:

Primitive arrays contain elements that are of primitive types such as int and boolean. Object arrays contain elements that are of reference types such as class instances and other arrays

In the first case, each array element is an int, which is a primitive type, resulting in a primitive array. In the second case, each element of the array is int[], which is an array and therefore an object (an array itself is an object).

Upvotes: 3

Related Questions