1. Member
Join Date
Oct 2011
Posts
19
Rep Power
0

## Double to Byte

Hi,

I am learning Java and I do not understand the output for Double to Byte conversion.

Java Code:
```class Conversion {
public static void main(String args[]) {
byte b;
int i = 257;
double d = 323.142;
System.out.println("\nConversion of int to byte.");
b = (byte) i;
System.out.println("i and b " + i + " " + b);
System.out.println("\nConversion of double to int.");
i = (int) d;
System.out.println("d and i " + d + " " + i);
System.out.println("\nConversion of double to byte.");
b = (byte) d;
System.out.println("d and b " + d + " " + b);
}
}```
The output is
Conversion of double to byte.
d and b 323.142 67

Why is output 67? any thoughts?

2. Member
Join Date
Oct 2011
Posts
92
Rep Power
0

## Re: Double to Byte

A byte can only hold from -127 - 127.

This is because it is 8 bits long. (otherwise known as a Byte!)

In an 8 bit binary number stored in two's compliment notation, the biggest value that can be represented is 127.

The reason you've ended up with 67 is that the Java runtime environment has lost a ton of bits trying to store 323 :)

3. Senior Member
Join Date
Jan 2012
Posts
210
Rep Power
3

## Re: Double to Byte

Originally Posted by christopherx
The reason you've ended up with 67 is that the Java runtime environment has lost a ton of bits trying to store 323 :)
And no exception at sight...

4. Member
Join Date
Oct 2011
Posts
19
Rep Power
0