Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

casting

 
Randall Twede
Ranch Hand
Posts: 4371
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I waited too long to compile and run this code so I forgot where it came from and what the explanation was.

public class lost {
public static void main(String args[])
{
byte b[] = {1,2,3};
byte d[];
Cloneable c = (Cloneable)b;
d=(byte [])c;
System.out.println(b);
}
}

why does it cause a compile error illegal cast from Cloneable to byte[]
 
Santhosh Kumar
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It compiling fine and printing criptic o/p [B@f0d30f02
I think this is address of the array.
Can somebody pl look into this?
Santhosh.
 
Randall Twede
Ranch Hand
Posts: 4371
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have jdk 1.3 it is compile time error
 
Vidya Selvaraj
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using jdk1.3 & it is compiling fine ( without any changes to the original pgm) & yes it is printing "[B@73d6a5"
[This message has been edited by Vidya Selvaraj (edited December 01, 2000).]
 
Randall Twede
Ranch Hand
Posts: 4371
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
all i did was change the class name cause i saved it with a different name. it gives me 2 compile time errors. invalid cast from Cloneable to byte[] and invalid statement
 
Randall Twede
Ranch Hand
Posts: 4371
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
perhaps i accidentally changed something:

public class Test2 {
public static void main(String args[])
{
byte b[] = {1,2,3};
byte d[];
Cloneable c = (Cloneable)b;
d=(byte [])c;
System.out.println(b);
}
}

I get 2 compile time errors
 
saumil shukla
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tested it on jdk 1.2 it prints memory address. I renamed the class and still got the output.
 
Randall Twede
Ranch Hand
Posts: 4371
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
like i said i forgot where i found this code but it gives 2 compile time errors here
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Randall,
I did a cut and paste of the code and it compiled fine under JDK 1.3.
Which lines are giving you the errors? Is there anything extraneous on those lines in your editor?

------------------
Jane
The cure for boredom is curiosity.
There is no cure for curiosity.
-- Dorothy Parker
 
Randall Twede
Ranch Hand
Posts: 4371
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the line that gives me the errors is
d=(byte [])c;
I am compiling from Forte IDE, but i wrote it in notepad.
i am also curious why the output you people get is what it is.
 
Amond Adams
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class lost {
public static void main(String args[])
{
byte b[] = {1,2,3};
byte d[];
Cloneable c = (Cloneable)b; //explicit (cast) for redundancy.
d=(byte [])c;
System.out.println(b);
}
}
As all arrays (of primitive or objects) implement Cloneable, Serializable and extends Object. Therefore arrays are castable (widening cast) into the above types.
byte[] b = {.....}
can be casted to Cloneable c = b;
or java.io.Serializable s = b;
However to reverse cast you'll need do as follows;
byte[] new_arr = (byte[]) c;

As for the statement System.out.println(b) which displays;
B]X@XXXXXXX is the array descriptor string.

 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Randall,
Might be a problem with Forte. Have you tried compiling it from the command line?

------------------
Jane
The cure for boredom is curiosity.
There is no cure for curiosity.
-- Dorothy Parker
 
Randall Twede
Ranch Hand
Posts: 4371
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jane,
I'm thinking it must be since it compiles for everyone else. I never got my CLASSPATH right in AUTOEXEC so I hate compiling from the command line but i should try just to see.
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Randall,
If you're using Win98 you might want to check some notes I made at http://webhome.idirect.com/~jgriscti/win98.html.
I had a horrible time, took me three days to get it working
------------------
Jane
The cure for boredom is curiosity.
There is no cure for curiosity.
-- Dorothy Parker
 
Randall Twede
Ranch Hand
Posts: 4371
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmm server not found page cannot be displayed. I will try it again later.
ok i got there and saved the page.
thanks Jane!
[This message has been edited by Randall Twede (edited December 04, 2000).]
 
Vladimir Kositsky
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all, just for statistics -
above code compiled without problems. JDK 1.22 + WIN 98
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic