If I have an integer that will have always a positive number less than 16 , can I just cast it to byte
int i = 5;
byte b = (byte) i;
or should I have unexpected behaviour when converting it back to integer on different devices?
Thanks
If I have an integer that will have always a positive number less than 16 , can I just cast it to byte
int i = 5;
byte b = (byte) i;
or should I have unexpected behaviour when converting it back to integer on different devices?
Thanks
You could always use the Integer class and use byteValue.
http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#byteValue()
which simply does
return (byte)value;
No, you won't get unexpected behaviour converting a byte
between 0 and 15 into an int
on different platforms. One of the strengths of Java is that it precisely defines what happens with such conversions, so that they are always platform independent.