wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Assigning/Casting Array types to objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Assigning/Casting Array types to objects" Watch "Assigning/Casting Array types to objects" New topic
Author

Assigning/Casting Array types to objects

Surya B
Ranch Hand

Joined: May 10, 2000
Posts: 98
Hi,
There is an example given in the JLS(Page 72 of PDF version) stating that it would compile but would throw ClassCastException
at runtime.Heres the code:
public class Point { int x, y; }
public interface Colorable { void setColor(int color); }
public class ColoredPoint extends Point implements Colorable
{
int color;
public void setColor(int color) { this.color = color; }
}
class Test
{
public static void main(String[] args)
{
Point[] pa = new Point[100];
// The following line will throw a ClassCastException:
ColoredPoint[] cpa = (ColoredPoint[])pa;
System.out.println(cpa[0]);
int[] shortvec = new int[2];
Object o = shortvec;
// The following line will throw a ClassCastException:
Colorable c = (Colorable)o;
c.setColor(0);
}
}
This class would not compile saying that
Test.java:8: Incompatible type for declaration. Can't convert int[] to Object.Object o=shortvec;
pointing the mistakes to the lines
int[] shortvec = new int[2];
Object o = shortvec;
Going by the JLS rule
If S is an array type SC[], that is, an array of components of type SC :
If T is a class type, then if T is not Object, then a compile-time error occurs(because Object is the only class type to which arrays can be assigned).
Since here the array is being assigned to object it should work fine,then why is it giving compile time error and why does JLS says that it will give runtime errors but not compile time.
Am i missing something here?.Please take time to answer this question.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944

Compiled for me. Also, gave me the exception.
I stored them in four diff files (shouldn't matter).
I am running Java 1.2.2 on Win98.
java version "1.2.2"
Classic VM (build JDK-1.2.2-W, native threads, symcjit)
I know this may not help but you might want to check
your code files ....
Regds.
- satya
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi,
i have jdk 1.2.2 and the code is behaving as per expectations. it compiles fine and gives a ClassCastException at runtime.

Rahul
Surya B
Ranch Hand

Joined: May 10, 2000
Posts: 98
Hi guys,
Thanks for the trouble you guys took of compiling the program,i don't know what's happening but when i copied the four files into a totally different directory on my machine,it compiled fine,but when i tried to compile in the original directory where the files were there it is still giving me a compile time error ,anyway thanks once again.
neo
Greenhorn

Joined: Aug 03, 2002
Posts: 8
Dear,
I think it won't work and the compiler error is the Expected result. RHE clearly says that arrays can only be casted if they are;
1. Arrays of Object ref. and not arrays of primitives.
2. The individual Object Ref. shud also be compatible with each other.
In your case it's an array of primitives being casted.
Pls. correct me if I'm wrong.

------------------
regards,
NEO
Keep on trying guys, the goal is not too far....
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Neo- RHE were talking about casting one array to another type of array. This example is talking about casting an array to an Object, which is always possible. (Note that the runtime error mentioned above is at a different line.)


"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Assigning/Casting Array types to objects
 
Similar Threads
Object[] to Long[]
ArrayStoreException
Overloaded method call
my notes on JLS for any1 who needs them !!
cast