Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is an array an object in Java?

Tags:

java

In Java we can declare an array using the following:

String[] array = new String[10];  int size = array.length;  

Does this mean that the array itself is an object? I ask, because in C++ an array is just a pointer and does not have any methods.

like image 800
Mike G Avatar asked Jan 08 '12 20:01

Mike G


People also ask

Is array in Java primitive or object?

No, arrays are not primitive datatypes in Java. They are container objects which are created dynamically. All methods of class Object may be invoked on an array. They were considered as reference data types.

Why array in Java is an object?

Array is considered to be an object in Java. The reason behind this is that an array can be created using the 'new' keyword. The 'new' keyword/operator is always used to create an object. This is how an array is perceived as an object.

Is an array a type of object?

An array can be considered to be an object with the following properties/keys: Length - This can be 0 or above (non-negative). The array indices. By this, I mean "0", "1", "2", etc are all properties of array object.

Is an array always an object?

Yes, the docs say so: An array is a container object that holds a fixed number of values of a single type. Note that array types of primitive types (like int[] or char[] ) themselves are also objects.


2 Answers

Yes.

The Java Language Specification section 4.3.1 starts off with:

An object is a class instance or an array.

like image 80
Paul Avatar answered Sep 22 '22 08:09

Paul


Yes; the Java Language Specification writes:

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.

like image 20
meriton Avatar answered Sep 22 '22 08:09

meriton