array is composite data type but how?

Viewed 2826

Array is composite data type. A composite data type is one whose values are composed of component values (possibly values chosen from other data types.) Example of composite data type is array.

int a[ ] = {1,2,3,4,5};

In above example, as far as I understand it, is composite because an array-of-int value is comprised of some number of element values chosen from the int type. Using composite data types, we can manage multiple pieces of related data as a single datum. An array is the concept of using a number of values declared with in the same data type.

Please explain in simple way array is composite data type. Explain this statement with example?

1 Answers

A class defined with various primitive data types such as int, double etc;but i am using only one data type for array initialization that is int

Consider the following example:

class Test {
    int a, b, c;
    Test() {
        a = 1 ; b = 2 ; c = 3;
    }
}

class Main {
    public static void main(String[] args) {
        Test object = new Test();
    }
}

As per your definition, in this case, even the class Test will be a primitive data type because it only int type values. However, this is not true.

As @Henry stated,

The element types are irrelevant, important is only that there are several elements

So basically, I would say a more precise definition for composite data type would be:

The data types which are created by the user, which may consist of one or more primitive data types, created under a single declaration are called Composite data types.

Going by that definition, arrays are also composite data types.

Related