Java byte type

In this chapter you will learn:

  1. How to use Java byte type
  2. Size and value for Java byte type
  3. Example - Java byte type

Description

The smallest integer type is byte. byte type variables are useful when working with a stream of data from a network or file.

Byte variables are declared by use of the byte keyword. The following declares two byte variables called b and c:

byte b, c;

Size and value

byte is a signed 8-bit type that has a range from -128 to 127.

Example

The following code creates two byte type variables and assigns values.


public class Main {
  public static void main(String[] args) {
    byte b1 = 100;
    byte b2 = 20;
    System.out.println("Value of byte variable b1 is :" + b1);
    System.out.println("Value of byte variable b1 is :" + b2);
  }//from   w ww.  ja va 2s. c om
}

The code above generates the following result.

The Byte class wraps a value of primitive type byte in an object. Byte class provides several methods for converting a byte to a String and a String to a byte.

Next chapter...

What you will learn in the next chapter:

  1. What is Java short type
  2. Size and value for Java short type
  3. Example - Java short type
Home »
  Java Tutorial »
    Java Langauge »
      Java Data Types
Java Primitive Data Types
Java boolean type
Java char type
Java char value escape
Java byte type
Java short type
Java int type
Java long type
Java float type
Java double type
Java String type
Java String Escape
Java String Concatenation