Byte

Primitive Data Types

Byte

🙋 Need help? Ask an expert now!

One of the least used primitive data types is byte because byte has a limited range of numbers (-128 to 127) compared to int. Unless the programmer are a 100% sure that the values will not exceed this limited range, it is best to use int. Nevertheless, let's take a look at how byte variables are declared and used below.

BytesDataTypeExample.java
package exlcode;

public class BytesDataTypeExample {

  // byte can be any integer between -128 and 127
  public static byte exampleVariableOne = 25;
  public static byte exampleVariableTwo = -25;
  public static byte exampleVariableThree = 127;

  public static void main(String[] args) {
    System.out.println(exampleVariableOne);
    System.out.println(exampleVariableTwo);
    System.out.println(exampleVariableThree);
  }
}

The byte datatype takes up about four times less space than an int, but is not used for calculations and other methods. The byte datatype is useful when dealing with raw binary data for compatibility reasons. Similarly to float, int and the other integer related data types, the default value for byte is 0. The byte datatype can only hold whole, non-decimal numbers.

Edit Me on GitHub!

Application Question

Which one of these is a valid byte declaration?