Byte With Examples And Program In JAVA

The smallest integer data type is byte. Byte type variables are especially useful when you are working with a stream of data from a network or a file. They are also useful when you are working with raw binary data that may not be directly compatible with Java’s other built-in types. Keyword “byte” is used to declare byte variables. For example, the following declares two byte variables called a and b: byte a, b;

Important Points About byte Integer Data Type:

  • Byte data type is an 8-bit signed two’s complement integer.
  • Minimum value of Byte is -128 (-27).
  • Maximum value of Byte is 127 (inclusive)(27 -1).
  • Default value of Byte is 0.
  • Byte data type is used to save memory in large arrays, mainly in place of integers because byte is four times smaller than an int.

Important Note: Remember value of byte data type size is -128 to 127


Table of Contents

byte Syntax in JAVA:

byte Variable_Name = Value;

For example:

byte x = 10;

Here x is variable name and 10 is a value assigned to a variable integer data type byte.


Byte Explanation With Example Program:

Example 1: This program prints the value of variables of type byte.

public class TestingByte {

	public static void main(String[] args) {
		
		byte b = 10; //declare a byte variable and assign the value i.e 10
		System.out.println(b); // print the value of byte variable b

	}

}

Output:

10

Example 2: byte Example using Byte Class:

You can see in the below program we use Byte class to create a object because if we call the variable values by class name then it gives us compilation error that arguments differ in length. That’s why we use Byte class.

More details about Byte class and its methods is shared after Example.

public class Number

{

    public static void main(String[] args)

    {

        byte b = 10; //declare a byte variable and assign the value i.e 10

        Byte n1 = new Byte(b);
        Byte n2 = new Byte("4");

        System.out.println(n1); // print the value of byte variable n1        
        System.out.println(n2);//print the value of byte variable n2

    }

}

Output:

10
4

Example 3: Using byte variable for addition

In below example we use byte data type in class AdditionByte to add two numbers of type byte and stores the value in third variable of type byte:

public class AddtionByte {

    public static void main(String[] args) {
        byte a = 30;
        byte b = 40;    
        byte c = (byte) (a + b);

        System.out.println("The c variable Value after Addition is : " + c);

    }

}

Output:

The c variable Value after Addition is : 70


Byte Class in JAVA:

The java.lang.Byte class wraps a value of primitive type byte in an object. Object of Byte type contains a single field whose type is byte.

Declaration of java.lang.Byte Class:

Following is the declaration of java.lang.Byte class:

public final class Byte extends Number implements Comparable<Byte>

Fields of java.lang.Byte Class:

Following are the fields of java.lang.Byte class:

  1. static byte MAX_VALUE : This is constant which holds maximum value of byte i.e 27-1.
  2. static byte MIN_VALUE : This is constant which holds minimum value a byte i.e-27.
  3. staticint SIZE : This is the number of bits used to represent a value of byte in two’s complement binary form.
  4. static Class<Byte> TYPE : This is the Class instance which represents the primitive type byte.

Constructors of java.lang.Byte class:

Following are the constructors of java.lang.Byte class:

  1. Byte(byte value):This constructs a newly allocated Byte object representing the specified value of byte.
  1. Byte(String s):This constructs a newly allocated Byte object representing the value of byte indicated by the String parameter.

Methods of java.lang.Byte class:

Following are the methods of java.lang.Byte class:

  1. bytebyteValue(): This method returns thebyte value of this Byte.
  2. intcompareTo(Byte anotherByte): This method compares two Byte objects numerically.
  3. static Byte decode(String nm): This method decodes a String into a Byte.
  4. doubledoubleValue(): This method returns thedouble value of this Byte.
  5. boolean equals(Object obj): This method compares this object to the specified object.
  6. inthashCode(): This method returns a hash code for this Byte.
  7. floatfloatValue(): This method returns thefloat value of this Byte.
  8. intintValue(): This method returns theint value of this Byte.
  9. longlongValue(): This method returns thelong value of this Byte.
  10. static byte parseByte(String s): This method parses the string argument as a signed decimal byte.
  11. static byte parseByte(String s, int radix): This method parses the string argument as a signed byte in the radix specified by the second argument.
  12. shortshortValue(): This method returns theshort value of this Byte.
  13. String toString(): This method returns a String object that represents the value of Byte.
  14. static String toString(byte b): This method returns a new String object which represent the specified byte.
  15. static Byte valueOf(byte b): This method returns a Byte instance which represent the specified byte value.
  16. static Byte valueOf(String s): This method returns a Byte object that holds the value given by the specified String.
  17. static Byte valueOf(String s, int radix): This method returns a Byte object that holds the value taken from the specified String when parsed with the radix given by the second argument.