class hello 
{
   
}
class hello1
{
   public static void main(String aa[])
   {
      hello ob[]={new hello(),new hello(),new hello()};
      hello ob1[]=ob.clone();           //  1
   }
}

ob is an array which is also treated as an object, and hence has a default clone method which is called at (1). But the default clone method returns an Object reference, so that should not be assignable to an array of type hello. But its being assigned here to an array of hello type without errors. Why ?

Recommended Answers

All 6 Replies

Is this some more of the autoboxing type of help the compiler gives us?

Because arrays get special treatment as per the specs:

The public method clone, which overrides the method of the same name in class Object and throws no checked exceptions. The return type of the clone method of an array type T[] is T[].

commented: Nice find. +14

ob is an array which is also treated as an object, and hence has a default clone method which is called at (1). But the default clone method returns an Object reference, so that should not be assignable to an array of type hello. But its being assigned here to an array of hello type without errors. Why ?

An array is-an Object and therefore you can call methods of the Object class on an array, which inherits these methods. However, as you can tell by writing a short program, the array class overrides the clone method. Therefore when you call clone() on an array, it does not simply return the same Object reference, but it actually creates another array.

Hello ob[]={new Hello(),new Hello(),new Hello()};
Hello ob1[] = ob.clone();
if (ob == ob1) System.out.println("Same");
else System.out.println("Not the same");

The above code prints not the same, which to me, indicates that a new array Object has been created. The exact details of what the clone() method does for the array class I don't know, or really care. You will note, however, that if you print out

if (ob[0] == ob1[0]) System.out.println("ob the same");

it will indeed print out that they're the same, which is because in that case, your Hello class does not override clone, so it just returns the reference.

Because arrays get special treatment as per the specs:

Why is that special treatment? Because they actually overrode clone..? Do they not usually do that?

edit: jumping ahead of myself, trying to catch you before you got out of the thread.

Why is that special treatment?

Because arrays themselves are special. For e.g., every class which supports cloning (implements Cloneable) provides a public implementation for the clone method which shows up when you iterate through the methods of that class.

for(Method m : ArrayList.class.getMethods()) {
    System.out.println(m.getName());
}

prints out the name "clone" because ArrayList implements Cloneable and provides an implementation of the clone() method. This is again true for every other class which implements Cloneable except for arrays. I.e.

for(Method m : String[].class.getMethods()) {
    System.out.println(m.getName());
}

does not print out the "clone" method even though the Javadoc of the "clone()" method of Object class specifically says:

[...] Note that all arrays are considered to implement the interface <tt>Cloneable</tt>[...]

Notice the play of words here; it says "considered" and not "actually" implement the Cloneable interface. I guess this was expected for a language construct which can be assigned to a reference type but doesn't have a programmer accessible class. I'm sure carefully thinking about how arrays are used in Java would give you a few more points as to *why* arrays in Java are special. :-)

Thanks a lot. I got it.

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.