Home > Blockchain >  Array = complexe data type = class?
Array = complexe data type = class?

Time:11-14

I learned that there are two types of data types (complex and primitive). The difference should lie in the fact that the complex data types have or can output more than one content. I have noted String or the scanner as examples.

I also learned that complex data types are nothing more than classes. The assignment using "new" then creates objects from these classes / complex data types, which in turn can be referred to as objectives or complex variables.

Now, with the array, I am not sure whether this is the same or should be understood differently. Because with the array (at least in java) an additional declaration is made, for example int [] = new int [6]; Since it looks so different to other complex data types and arrays are also referred to as data structures, I wanted to know whether I understood everything correctly and whether it can also be transferred to the array.

CodePudding user response:

In java, arrays are objects, even if it's an array of primitive type.

For exemple :

int[] array = {0,1,2,3};
// is the same as :
int[] array = new int[4];
array[0]=0;
array[1]=1;
array[2]=2;
array[3]=3;
// array[0] is primitive (int) but array is object

CodePudding user response:

In the Java programming language, arrays are objects (§4.3.1), are dynamically created, and may be assigned to variables of type Object (§4.3.2). All methods of class Object may be invoked on an array.

From Oracle docs

CodePudding user response:

In java array is just an object ($4.3.1) and you can check it your self by with a simple main method

public static void main(String[] args) {
    int[] array = new int[]{1,2,3};
    System.out.println(array.getClass().isPrimitive()); // returns false
}
  • Related