"byte" Data Type and Implicit Casting

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:

To verify how values can be assigned to "byte" data type variables, I wrote the following sample program, ByteValues.java:

/* ByteValues.java
 - Copyright (c) 2014, HerongYang.com, All Rights Reserved.
 */
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:

b1: 127
b2: 127
b3: 127
b4: 127
b5: 127
b6: 127
b7: 127
b8: 65

The output shows that:

"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.8:

ByteValues.java:29: error: incompatible types: 
   possible lossy conversion from int to byte
      d1 = 128; // out of range
           ^
ByteValues.java:30: error: incompatible types: 
   possible lossy conversion from int to byte
      d2 = -129; // out of range
           ^
ByteValues.java:31: error: incompatible types: 
   boolean cannot be converted to byte
      d3 = (byte) (1==1); // type not compatible
                  ^
ByteValues.java:32: error: incompatible types: 
   String cannot be converted to byte
      d4 = (byte) "A"; // type not compatible
                  ^
ByteValues.java:33: error: incompatible types: 
   <null> cannot be converted to byte
      d5 = (byte) null; // type not compatible
                  ^
ByteValues.java:34: error: cannot find symbol
      d6 = (byte) new Date(); // type not compatible
                      ^
  symbol:   class Date
  location: class ByteValues
6 errors

The error messages are so clear. I don't need to add any explanations.

Note that compilation error message format has been changed from JDK 1.7 to JDK 1.8. Here is what you will get with JDK 1.7:

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

Last update: 2014.

Table of Contents

 About This Book

 Installing JDK 1.8 on Windows

 Execution Process, Entry Point, Input and Output

 Primitive Data Types and Literals

Bits, Bytes, Bitwise and Shift Operations

 What Are Bits and Bytes

"byte" Data Type and Implicit Casting

 Operations on "byte" Data Type Values

 Bitwise Operations on "byte" Values

 Bitwise Operations on "byte" Values - Example Program

 Shift Operations - Left, Right or Unsigned Right Shift

 Managing Bit Strings in Byte Arrays

 Reference Data Types and Variables

 StringBuffer - The String Buffer Class

 System Properties and Runtime Object Methods

 Generic Classes and Parameterized Types

 Generic Methods and Type Inference

 Lambda Expressions and Method References

 Execution Threads and Multi-Threading Java Programs

 ThreadGroup Class and "system" ThreadGroup Tree

 Synchronization Technique and Synchronized Code Blocks

 Deadlock Condition Example Programs

 Garbage Collection and the gc() Method

 Outdated Tutorials

 References

 PDF Printing Version