3

In Java, it is possible to assign an array to an Object Reference.

class Sample{
     public void access(){
     int a[]= {1,2,3,4};
     Object oRef = a;
  }
}

What does it mean for a reference to refer to an array? An array is a number of elements while a reference is a single element. So what is the meaning of this reference? The second question is, since it is possible to assign a reference to an array, is there a way to access the elements of the array using the reference?

kauray
  • 739
  • 2
  • 12
  • 28
  • 1
    The reference is the address where the variable is in the heap. When you cast to `Object`, you lose the functionality of an array. You can regain it by explicitly casting back to `int[]`. – Tim Biegeleisen Nov 27 '15 at 05:48
  • @TimBiegeleisen the oRef is a singluar entity, whereas the array comprises multiple elements. So what exactly is happening when a many element entity is being assigned to a singular one? Is it referring to the first element or the entire array as such? – kauray Nov 27 '15 at 06:29
  • see this for example about objects and "references": http://stackoverflow.com/questions/869033/how-do-i-copy-an-object-in-java . What problem do you want to solve ? – guillaume girod-vitouchkina Nov 27 '15 at 08:27

4 Answers4

1

Before starting with the answers to your questions, lets get it clear that arrays are "Objects" in Java. So if you say int a[]= {1,2,3,4};, you are creating an Object of type int array, and to refer to that object, you will be using the reference a. So now, lets to your questions :

1) What does it mean for a reference to refer to an array? An array is a number of elements while a reference is a single element. So what is the meaning of this reference?

enter image description here

In the above image, a is a reference, which is stored on the stack, whereas the actual array,i.e. the object to which a refers, is stored on heap. There is a class for every array type, so there's a class for int[]. If you are curious, here's a code snippet that will prove my above statement :

public static void main(String[] args)
{
    test(int[].class);
}

static void test(Class clazz)
{
    System.out.println(clazz.getName());
    System.out.println(clazz.getSuperclass());
    for(Class face : clazz.getInterfaces())
        System.out.println(face);
}

Credits for the above snippet. Now, it is clear that JVM makes these classes itself at runtime. And every class in java, is a subtype of the Object class. Hence, a reference of type Object can refer to the instance of type int[] I hope this clears the first part of the question. Now, the second part,

2) Is there a way to access the elements of the array using the reference?

Answer : Yes it is. The way is :

int c[] = (int[])oRef;

When you write the above statement, you create a reference of type int[], and you make that reference point to the previously created object of type int[]. By saying (int[])oRef, you are just typecasting the reference of type Object which is a super class to int[] which is the subclass. So now the above picture will change to :

enter image description here

Hope that answers both your questions.

Community
  • 1
  • 1
Sagar D
  • 2,588
  • 1
  • 18
  • 31
0

No you can not access the elements of the array using the reference oRef. Everything in java is an object except primitives, so yes you can have a reference of object to refer to an array.

But you are going up in hierarchy so you will lose all the benefits of array i,e. accessing the elements by specifying position and all.

Now by using this reference you have access to the bare minimum methods available for object i.e. toString().equals() etc.

Siyual
  • 16,415
  • 8
  • 44
  • 58
RockAndRoll
  • 2,247
  • 2
  • 16
  • 35
0

Once you start referring to an instance by another reference (without any compilation errors), that another reference becomes that instance's type in that scope. For example, Consider the following snippet

class B extends A{}

Class Test{

B instance = new B(); //line 3
A refOfA = (A) instance; //line 4
}

Now at line 3 the instance is an instance of class B and the type of instance is B. At line 4, the instance is still an instance of class B, but is now typecast to A and the type of reference to that instance becomes A. So in effect, you can access A's members using refOfA.

When you are referring to the array with an object reference, a similar thing happens.

You may want to read about overriding, overloading, and the discussion about type and object oriented concepts like interface, etc from the Gang Of Four Design Patterns book.

Atul
  • 2,673
  • 2
  • 28
  • 34
0

All arrays are considered instances of Object, so any array can be converted to an Object value through a widening conversion. A narrowing conversion with a cast can convert such an object value back to an array.

For example:
Object o = new int[] {1,2,3};  // Widening conversion from array to Object
int[] a = (int[]) o;           // Narrowing conversion back to array 

For the second question, it is possible to assign a reference to an array only when you assign back an object to array(using int[] casting) as below, After assigning the oRef to integer array c[], you can access the elements of an array.

int a[]= {1,2,3,4};
Object oRef = a;
int c[] = (int[])oRef;
System.out.println("element of c:"+c[0]);
Priya
  • 81
  • 3
  • The second question is answered. The first question is not. I asked "An array is a number of elements while a reference is a single element. So what is the meaning of this reference?" What exactly is happening when a many element entity is being assigned to a singular one, that is, the array and the Object reference in this case? Your answer provides 'Why' not 'What'. – kauray Nov 27 '15 at 06:34