-2

Every object in Java has a prototype (class) and objects are instances of those prototypes. If i declare an array int a[] = new int[5]; this will create an object which can hold 5 integers in heap referenced by a. Do we have any prototype of this object? What are the state and behaviour of the array objects? Is length considered as a array object's state?

Pankaj
  • 14,638
  • 3
  • 17
  • 23

3 Answers3

1

Is an array an object in java

If you're coming from javascript, prototypes are not entirely the same as classes. It would be better if you did not call classes prototypes

Every object in java has a prototype (class)

Every object in java is an instance of a class, but not everything in java is an object. For efficiency reasons, int, boolean, float, double, long and short (forgive me if I missed one) are primitives; they are not objects. Arrays are also not regular objects. An array is not an instance of any particular class, but it is still an object. To check if something is an array you would do this:

int arr[] = new int[9];
if (arr instanceof int[]) {
    System.out.println("It's an array of ints!");
}

The above code should output: It's an array of ints!

EDIT: I realised you may have been asking about JavaScript and not Java. If so, then in JavaScript, arrays are instances of the Array prototype.

EDIT: From the javaspec

10.1. Array Types ... The supertypes of an array type are specified in §4.10.3.

The direct superclass of an array type is Object.

Every array type implements the interfaces Cloneable and java.io.Serializable.

and

4.10.3. Subtyping among Array Types

The following rules define the direct supertype relation among array types:

If S and T are both reference types, then S[] >1 T[] iff S >1 T.

Object >1 Object[]

Cloneable >1 Object[]

java.io.Serializable >1 Object[]

If P is a primitive type, then:

    Object >1 P[]

    Cloneable >1 P[]

    java.io.Serializable >1 P[]

So an array is a special type of Object it doesn't have a identifiable class, but it still has a type. It's place in the Class Hierarchy and it's methods are defined in the spec.

Read http://docs.oracle.com/javase/specs/jls/se7/html/jls-10.html for more detail

Community
  • 1
  • 1
Moyamo
  • 358
  • 2
  • 10
1

Yes. It has a prototype because it is an instance of Object class.

    Integer a[] = new Integer[5];
    int b[] = new int[5];

    if (b instanceof Object) {
        System.out.println("YES");
    } else {
        System.out.println("NO");
    }
    if (a instanceof Object) {
        System.out.println("YES");
    } else {
        System.out.println("NO");
    }

Output :

YES

YES

I think you can check the states and behaviors using the reference if you are using an IDE. And also i think length can be considered as a state. Check this.

enter image description here

prime
  • 14,464
  • 14
  • 99
  • 131
  • read [this](http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html) to get an understanding of Object class – prime Dec 13 '13 at 06:39
0

You should not use word prototype in current context as @Eliiott said specifically JavaScript has prototypes. But from the concept of class, according oops principles, state of an object is defined as the value of its instance variables, so length will surely come under array object's state because at any length is a field inside array class, And behavior of an object is what it do(its methods), so all the instance methods array class have would be the behavior of an array object.

Abhishek Singh
  • 943
  • 8
  • 14