Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't there a java.lang.Array class? If a java array is an Object, shouldn't it extend Object?

Here is the java package-tree: http://docs.oracle.com/javase/7/docs/api/java/lang/package-tree.html

I read a tutorial on Java which stated that in Java arrays are objects.

Where is the array class? How comes we can make arrays like this:

byte[] byteArr = new byte[]; char[] charArr = new char[]; int[] intArr = new int[]; 

and the arrays will inherit methods from Object; for example:

    byte thisByte = 1;     byte thatByte = 2;     byte[] theseBytes = new byte[] {thisByte, thatByte};     int inheritance = theseBytes.length; //inherited 'length' field and some methods     int wasntInWill = thatByte.length; //error 

What's going on here?

EDIT:

As per the answers, I now know it is a final class in java.lang.reflect package.

I have now created a package java.lang.reflect in my Android project and have added a class called Array.java to it. To confirm this is in the way of the original class, Eclipse gave me the error "... already exists in path/to/android.jar"

If I write out the same class as java.lang.reflect.Array but change the toString() method... this should work within my application right?

like image 266
Ozzy Avatar asked Dec 17 '11 17:12

Ozzy


People also ask

Is there an array class in Java?

The Array class provides static methods to dynamically create and access Java arrays. Array permits widening conversions to occur during a get or set operation, but throws an IllegalArgumentException if a narrowing conversion would occur.

Is a Java array always an object?

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.

Can an object contain an array Java?

Answer: Yes. Java can have an array of objects just like how it can have an array of primitive types. Q #2) What is an Array of Objects in Java? Answer: In Java, an array is a dynamically created object that can have elements that are primitive data types or objects.

Why do we need array of objects in Java?

Java programming language is all about classes and objects as it is an object-oriented programming language. When we require a single object to store in our program we do it with a variable of type Object. But when we deal with numerous objects, then it is preferred to use an Array of Objects.


1 Answers

From JLS:

Every array has an associated Class object, shared with all other arrays with the same component type. [This] acts as if: the direct superclass of an array type is Object [and] every array type implements the interfaces Cloneable and java.io.Serializable.

This is shown by the following example code:

class Test {     public static void main(String[] args) {         int[] ia = new int[3];         System.out.println(ia.getClass());         System.out.println(ia.getClass().getSuperclass());     } } 

which prints:

class [I class java.lang.Object 

where the string "[I" is the run-time type signature for the class object "array with component type int".

And yes, since array types effectively extend Object, you can invoke toString() on arrayObject also see the above example

int arr[] = new arr[2]; arr.toString(); 
like image 135
jmj Avatar answered Nov 04 '22 04:11

jmj