3

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?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
manu
  • 33
  • 2
  • 8
  • 3
    You've just explained it yourself. What are you unsure about exactly? – akuzminykh Aug 05 '20 at 16:22
  • A composite data type is one which is composed with various primitive data type. 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 – manu Aug 05 '20 at 16:24
  • The element types are irrelevant, important is only that there are several elements. – Henry Aug 05 '20 at 16:28
  • just rewording - an array is a composition of same type of values i.e. `int[] iArray = new int[5] ;` is same as a composition of type `aIntCompose { int i1, i2,i3,i4,i5 }` – PrasadU Aug 05 '20 at 16:31
  • 1
    When someone talks about "composite" I think about things like [composition over inheritance](https://en.wikipedia.org/wiki/Composition_over_inheritance) or [composite pattern](https://en.wikipedia.org/wiki/Composite_pattern). I've never thought about calling an array a composite but actually, per definition of the word, it is one. The explanation you've provided seems absolutely logical to me. But you should see arrays then as a composition of elements and not a composition of types; then the confusion is probably gone. – akuzminykh Aug 05 '20 at 16:34
  • you are taking instantiation as example for composition. may i take example of initialization of an array a [] = {1,2,3,4}?@PrasadU – manu Aug 05 '20 at 16:35

1 Answers1

0

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.

nltc
  • 83
  • 2
  • 10