import java.util.*;
public class Elway {
    public static void main(String r[]) {
        ArrayList[] ls = new ArrayList[3];
        for(int i=0; i<3; i++) {
            ls[i] = new ArrayList();
            ls[i].add("a" + i);  }

        Object o = ls;
        do3(ls);
        for(int i=0; i<3; i++)
            //insert code here
        }

    static Object do3(ArrayList[] a) {
        for(int i=0; i<3; i++) a[i].add("e");
        return a;
    }
}

The code above compiles fine with:
a. System.out.println(((Object[])o)[i] + " ")
b. System.out.println(((ArrayList[])o)[i] + " ")
but not with
c. System.out.println(o[i] + " ")

What is the reason? When we assigned ls to o, didn't Object o become an array?

Recommended Answers

All 4 Replies

When we assigned ls to o, didn't Object o become an array?

Don't lose track of the difference between objects and reference variables. Nothing you can do in Java will change the actual type of an object. ALl you can change is what references are known to refer to.
o is a ref var that can hold a reference to any object, including an array of ArrayLists. It can also hold a ref to a String, Integer, URL etc, which is why o[i] won't compile - unless it happens to be refering to an array at that particular time its wrong, and there's no general way for the compiler to know what it will be referring to at runtime.
In your first two cases you explicitly cast the ref to be a ref to an array, so the compiler believes that you can index it, but will compile in a runtime check, just to be sure.

Great thanks james! On the topic of objects and reference variable I had one more doubt.

public class Swanky {
    Swanky s;
    public static void main(String r[]) {
        Swanky s1 = new Swanky();
        s1.s = new Swanky();
        go(s1.s);
        Swanky s2 = go(s1);
    }

    static Swanky go(Swanky s) {
        Swanky gs = new Swanky();
        gs.s = s;
        return gs;
    }

In this extremely confusing code for me, the first invocation go(s1.s) once enters the go() creates a set of ref var and objects namely two ref var s, gs and one object. That is fine. Now what I want to udnerstand is when second go(s1) call happens and it enters the method, this time it creates separate s and gs ref var or re-uses the first time created one?

I think you lost count somewhere.

First let's count the ref vars:
Every instance of a Swanky has one var s.
main has two local vars, s1 and s2.
go has one local var, gs, which is created each time you enter go, and destroyed at the end of go's execution. A copy of this reference is returned from the method; the caller can ignore this (line 6) or keep its own copy (line 7)

Now let's count the objects:
main calls new Swanky twice - two new Swankys, containing two new vars.
go calls new Swanky once each time it's called - each Swanky has one new var.
Instances of any class are garbage collected when there are no remaining references to them

Aermed with that, and a sheet of paper, you should now be ble to draw all the objects and references in a single diagram and update that as you step manually thru the code... after which the correct answers will be obvious to you. :)

Hey james thanks yet again for a lucid explanation. Ya I have been using pen and paper for drawing links between ref vars and created objects.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.