hi, the following program gives -1 as o/p. while i was thinking it will raise an exception commented line. coz here after the division operation d1 has got 'infinity' as its value. so how we r putting infinity in a byte. class Question { public static void main(String[] args) { double d1 = 1.0; double d2 = 0.0; byte b = 1; d1 = d1 / d2; b = (byte) d1; //// commented line System.out.print(b); } }
regards deekasha
Anonymous
Ranch Hand
Joined: Nov 22, 2008
Posts: 18944
posted
0
when you cast a double to a byte, only the lower order bits are copied to the byte variable. When a double has a postitive infinity, the lower order bits are 1111 1111. When a double has a negative infinity, the lower order bits are 0000 0000. So the following code will assign 1111 1111 to b. 1111 1111 is -1 in decimal.