File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes casting interface to array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "casting interface to array" Watch "casting interface to array" New topic
Author

casting interface to array

Jauhar Ali
Greenhorn

Joined: Oct 03, 2000
Posts: 3
Hello everyone,
I am new to Javaranch and this is my first posting. Actually, I wanted to report a possible error in RHE.
On page 117, Fig:4.10 (Table) Row 4, Column 3, it is stated that casting from an interface to an array is compilation error. In the table of Fig4.9 (about convertion rules), it is stated that convertion from an array to an interface is legal provided that the interface should be Serialzable or Cloneable. I thought if "converting" from an array to an interface is legal then logically "casting" in the opposite direction should also be legal. I tested as follows and the code compiled.

Sorry, if this is already reported. But I could not find it in the erreta page of RHE.
Ali
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Jauhar,
Welcome to JavaRanch ; it's a great place!
I think your code works because <code>a</code> is really an <code>int[]</code>.
If you try the following you get a compile error:

The <code>interface Cloneable</code> is not compatible; can't be cast even though an array implicitly implements <code>Cloneable</code>
Jane


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
It is worth mentioning here that <code>Cloneable</code> has no methods in it. It just serves as a tag to indicate the particular object can be cloned. A class implements the Cloneable interface to indicate to the Object.clone() method that it is legal for that method to make a field-for-field copy of instances of that class.
Another such "stub-interface" is <code>java.io.Serializable</code>
Just my two cents worth...
Ajith


[This message has been edited by Ajith Kallambella (edited October 03, 2000).]


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Jauhar Ali
Greenhorn

Joined: Oct 03, 2000
Posts: 3
Originally posted by Jane Griscti:
[B]Hi Jauhar,
Welcome to JavaRanch ; it's a great place!
I think your code works because <code>a</code> is really an <code>int[]</code>.
If you try the following you get a compile error:

The <code>interface Cloneable</code> is not compatible; can't be cast even though an array implicitly implements <code>Cloneable</code>
Jane[/B]

Hi Jane,
In your code, you tried to cast an "array of Cloneable" to an "array of int" which surely gives compile error. But I meant that casting a "Cloneable" or "Serializable" to any array type is NOT a compile error. e.g, the following code compiles successfully.

Of course, there is a runtime error (ClassCastException), but compilatoin is OK.
Jauhar
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Ooops ... sorry Jauhar. Based on your example, I thought you were trying to make the point that a Cloneable array could be cast to any array as the array type implicitly implements Cloneable. Sorry for the mixup.
You are right, you can cast an Interface to an array; at least, at compile time

------------------
Jane
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: casting interface to array