Byte in Java

In this sample TryByte.java, you will learn how to define a variable that has byte type. You will also learn how to set its value.

Save following content in a .java file called TryByte.java. Note that the file name is equivalent to your class name TryByte.

public class TryByte {
   public static void main(String[] args) {
      byte minByte = -128;
      System.out.print("minByte value: ");
      System.out.println(minByte);

      byte maxByte = 127;
      System.out.print("maxByte value: ");
      System.out.println(maxByte);
   }
}

Using javac command compile your .java file

$ javac TryByte.java

And using java command run it. You will see two printed phrases there which shows two values for two byte variables minByte and maxByte. The value of minByte has the minimum value that a byte variable can have and that is -128, while the value of maxByte is true has the maximum value that a byte variable can have.

$ java TryByte
minByte value: -128
maxByte value: 127