GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes casting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "casting" Watch "casting" New topic
Author

casting

Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

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[]


SCJP
Visit my download page
Santhosh Kumar
Ranch Hand

Joined: Nov 07, 2000
Posts: 242
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

Joined: Oct 21, 2000
Posts: 4347
    
    2

i have jdk 1.3 it is compile time error
Vidya Selvaraj
Greenhorn

Joined: Nov 21, 2000
Posts: 29
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

Joined: Oct 21, 2000
Posts: 4347
    
    2

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

Joined: Oct 21, 2000
Posts: 4347
    
    2

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

Joined: Dec 01, 2000
Posts: 47
I tested it on jdk 1.2 it prints memory address. I renamed the class and still got the output.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

like i said i forgot where i found this code but it gives 2 compile time errors here
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
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


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

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

Joined: Nov 28, 2000
Posts: 62
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

Joined: Aug 30, 2000
Posts: 3141
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

Joined: Oct 21, 2000
Posts: 4347
    
    2

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

Joined: Aug 30, 2000
Posts: 3141
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

Joined: Oct 21, 2000
Posts: 4347
    
    2

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

Joined: Nov 03, 2000
Posts: 116
hi all, just for statistics -
above code compiled without problems. JDK 1.22 + WIN 98
 
GeeCON Prague 2014
 
subject: casting