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   ww  w .j  a  va  2  s  . com*/
}

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.





















Home »
  Java Tutorial »
    Java Language »




Java Data Type, Operator
Java Statement
Java Class
Java Array
Java Exception Handling
Java Annotations
Java Generics
Java Data Structures