This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

casting interface to array

 
Jauhar Ali
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3141
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ajith Kallambella
Sheriff
Posts: 5782
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
Jauhar Ali
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3141
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic