• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

casting problem

 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this is from jls 2.0 chap.5.5:and I think its a mistake in their text
" The detailed rules for compile-time correctness checking of a casting conversion of a value of compile-time reference type S (source) to a compile-time reference type T (target) are as follows: (T)S
....
* If S is an interface type:
o If T is an array type, then T must implement S, or a compile-time error occurs. "
(WRONG !!! IF ITS AN ARRAY TYPE YOU ALWAYS GET ERROR !!!)
the example below:
class T implements S
{
public void foo(){System.out.println("it works");}
}
interface S{void foo();}
class TestCast {
public static void main(String args[]){
T[] bigT=new T[100];
T t;
bigT=(T[])s;
}
}
gives a compile error
Can please someone comment on this...
 
Ranch Hand
Posts: 168
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The 's' variable in the line I gave a comment above hasn't been declared and initialized.
 
Dan Andrei
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
SORRY I MISSED SOME LINES OF CODE HERE IT IS:
class T implements S
{
public void foo(){System.out.println("it works");}
}
interface S{void foo();}
class TestCast {
public static void main(String args[]){
T[] bigT=new T[100];
S s=null;
bigT=(T[])s;
}}
 
Yosi Hendarsjah
Ranch Hand
Posts: 168
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't cast a non-array object to array type.
Change this line:

to

Your code will compile.
 
Dan Andrei
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I knew that... however the JLS 2.0 chap 5.5 thinks otherwise
that was I posted the question in the first place...
So its a mistake in the JLS 2.0 docs ?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic