Java Programming Home Page: Archive: Message #22

Date: Feb 08 2000 12:03:27 EST
From: "Java Programming" <javaProgramming-owner@listbot.com>
Subject: Casting

Hi All,
Some of you have asked me about casting.
Here is the scoop:
int i = 10;
long l = 100;
Since long is bigger than int, you can set
a long to an int value, without casting:
l = i;

However, data will be lost if you go the other way around:
i = l; // does not compile!!

So casting is required:
i = (int)l;

Then the extra bits are truncated and the setting is completed....
But be careful, as data is lost!!

With reference data-types casting works when to cast
a sub-class to a super class or to an interface datatype
implemented by the class.

Check out pp. 396 of the book for more details.
Or see:
http://www.docjava.com/java/java%20lects/ub/CS410x/lect4/lect4.htm

Regards,
 - DL