• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Assigning/Casting Array types to objects

 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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....
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.)
reply
    Bookmark Topic Watch Topic
  • New Topic