import java.awt.*; import java.applet.*; /* The following program demonstrates some type conversions that require casts. */ // public class Conversion extends Applet{ public void paint(Graphics g) { byte b; short s; int i = 257,j=20; double d = 323.142; g.drawString("Conversion of int to byte.",20,j); b = (byte) i; j+=20; g.drawString("i and b " + i + " " + b,20,j); j+=20; g.drawString("Conversion of Double to int.",20,j); i = (int) d; j+=20; g.drawString("d and i " + d + " " + i,20,j); j+=20; g.drawString("Conversion of Double to byte.",20,j); b = (byte) d; j+=20; g.drawString("d and b " + d + " " + b,20,j); j+=20; g.drawString("Conversion of int to short.",20,j); i = 32000; j+=20; s = (short) i; g.drawString("i and s " + i + " " + s,20,j); } } /* This program generates the following output Conversion of int to byte i and b 257 1 Conversion of double to int d and i 323.142 323 Conversion of double to byte d and b 323.142 67 When the value 257 is cast into a byte variable, the result is the remainder of the division of 257 by 256 (the range of byte) which is 1 in this case. When d is converted to an int, its fractional component is lost. When d is converted to a byte, its fractional component is lost, and the value is reduced modulo 256, which in this case is 67. */