This section provides a tutorial example on how 'byte' variables can be assigned with 'byte' values with implicit and explicit casting from other compatible types of values.
byte - A primitive data type in Java that can store 256 possible integer values from -128 to 127.
There is no explicit data literals for "byte" data type.
If you want to assign values to "byte" data type variables, you can:
Use the generic integer "int" data literals.
Use type cast operation "(byte)" to convert from other types of values.
Use expressions that result in integer (int) values.
To verify how values can be assigned to "byte" data type variables, I wrote the following
sample program, ByteValues.java:
/**
* ByteValues.java
* Copyright (c) 2008 by Dr. Herong Yang, http://www.herongyang.com/
*/
public class ByteValues {
public static void main(String[] arg) {
// Good "byte" assignments
byte b1, b2, b3, b4, b5, b6, b7, b8;
b1 = 127; // "int" literal and implicit casting
b2 = 0x0000007F; // "int" literal and implicit casting
b3 = (byte) 127; // "int" literal and explicit casting
b4 = (byte) 0x0000007F; // literal and explicit casting
b5 = 254/2; // "int" expression and implicit casting
b6 = 256127%256; // "int" expression and implicit casting
b7 = (byte) 127.14157; // "float" literal and explicit casting
b8 = (byte) 'A'; // "char" literal and explicit casting
System.out.println("b1: "+b1);
System.out.println("b2: "+b2);
System.out.println("b3: "+b3);
System.out.println("b4: "+b4);
System.out.println("b5: "+b5);
System.out.println("b6: "+b6);
System.out.println("b7: "+b7);
System.out.println("b8: "+b8);
// Bad "byte" assignments
byte d1, d2, d3, d4, d5, d6;
// d1 = 128; // out of range
// d2 = -129; // out of range
// d3 = (byte) (1==1); // type not compatible
// d4 = (byte) "A"; // type not compatible
// d5 = (byte) null; // type not compatible
// d6 = (byte) new Date(); // type not compatible
}
}
After executing this program, I got the following output:
Assignment operator performs implicit casting to convert the specified value to the same type as the target variable.
Converting other data type values to "byte" can be done by the explicit casting operator "(byte)".
"int", "float", and "char" are compatible with (can be casted to) "byte".
"ByteValues.java" also contains some hidden bad assignment statements. If you un-comment those statement,
you will get the following compilation errors with JDK 1.5:
ByteValues.java:29: possible loss of precision
found : int
required: byte
d1 = 128; // out of range
^
ByteValues.java:30: possible loss of precision
found : int
required: byte
d2 = -129; // out of range
^
ByteValues.java:31: inconvertible types
found : boolean
required: byte
d3 = (byte) (1==1); // type not compatible
^
ByteValues.java:32: inconvertible types
found : java.lang.String
required: byte
d4 = (byte) "A"; // type not compatible
^
ByteValues.java:33: inconvertible types
found : <nulltype>
required: byte
d5 = (byte) null; // type not compatible
^
ByteValues.java:34: cannot find symbol
symbol : class Date
location: class ByteValues
d6 = (byte) new Date(); // type not compatible
^
6 errors
The error messages are so clear. I don't need to add any explanations.