An array in Java is an object. Now the question how is this possible? What is the reason behind that? In Java, we can create arrays by using new operator and we know that every object is created using new operator. Hence we can say that array is also an object. Now the question also arises, every time we create an object for a class then what is the class of array?
- In Java, there is a class for every array type, so there’s a class for int and similarly for float, double etc.
- The direct superclass of an array type is Object. Every array type implements the interfaces Cloneable and java.io.Serializable.
- 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.
For every array type corresponding classes are available and these classes are the part of java language and not available to the programmer level. To know the class of any array, we can go with the following approach:
// Here x is the name of the array. System.out.println(x.getClass().getName());
NOTE:[I this is the class for this array, one [ (square bracket) because it is one dimensional and I for integer data type.
Here is the table specifying the corresponding class name for some array types:-
Array type Corresponding class Name int [I int [[I double [D double [[D short [S byte [B boolean [Z
In Java programming language, arrays are objects which are dynamically created, and may be assigned to variables of type Object. All methods of class Object may be invoked on an array.
This article is contributed by Bishal Kumar Dubey. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Primitive data type vs. Object data type in Java with Examples
- Program to convert Primitive Array to Stream in Java
- How to get slice of a Primitive Array in Java
- Widening Primitive Conversion in Java
- Primitive Wrapper Classes are Immutable in Java
- Comparison of double and float primitive types in Java
- Collator compare(Object, Object) method in Java with Example
- Merge arrays into a new object array in Java
- Type conversion in Java with Examples
- Comparison of boolean data type in C++ and Java
- Type Erasure in Java
- Local Variable Type Inference or LVTI in Java 10
- Basic Type Base64 Encoding and Decoding in Java
- How to create a Java HashMap of user defined class type?
- FileStore type() method in Java with Examples
- Finding Data Type of User Input using Regular Expression in Java
- Flexible nature of java.lang.Object
- Java Object Creation of Inherited Class
- Object class in Java
- Object Serialization with Inheritance in Java