Java Byte Class | Methods, Example Program

Byte class in Java is a wrapper class that wraps (converts) a value of primitive data type into an object.

In simple words, a Byte is a wrapper around a byte.

An object of Byte class contains a single byte type field. In this field, we can store a primitive type byte value. Note that the range of byte is from -128 to +127.

Byte class was introduced in JDK 1.1. It is present in java.lang.Byte package.

Byte class declaration


The general declaration of Byte class in Java is as follows:

public final class Byte
   extends Number
    implements Comparable<Byte>

Byte class extends Number class and implements Serializable and Comparable<Byte> interfaces. Number class extends Object class.

Byte Field Constants


Java Byte class provides several field constants that are as follows:

1. static int BYTES: It is the number of bytes that is used to represent a byte value in two’s complement binary form. It was introduced in JDK 1.8.

2. static byte MAX_VALUE: It represents a constant that can hold the maximum value of byte (2^7 – 1).

3. static byte MIN_VALUE: It represents a constant that can hold the minimum value of byte (-2^7).

4. static int SIZE: It is the number of bits that is used to represent a byte value in two’s complement binary form. It was introduced in JDK 1.5.

5. static Class<Byte> TYPE: It is a class instance that represents the primitive type byte.

Java Byte Class Constructor


Byte class in java provides two constructors that are as follows:

1. Byte(byte num): This form of constructor accepts byte number as its parameter and converts it into Byte class object. The general syntax to create Byte class object is as follows:

Byte obj = new Byte(120);

2. Byte(String str): This constructor accepts a parameter of String type and converts that string into Byte class object. The general syntax to create a Byte object by wrapping a string that contains a byte number, as follows:

Byte obj = new Byte("100");

Byte Class Methods in Java


In addition to methods inherited from Number class and Object class, Java Byte class also provides some useful important methods. They are as follows:

1. int compareTo(Byte b): This method is used to compare the contents of two Byte class objects numerically. It is called using the below syntax.

int x = obj1.compareTo(obj2);

where, obj1 and obj2 are Byte class objects.

  • If obj1 == obj2, this method returns 0.
  • If obj1 < obj2, it returns negative value.
  • If obj1 > obj2, it returns positive value.

2. boolean equals(Object obj): This method is used to compare Byte object with another Byte object obj. If both have the same contents, this method returns true otherwise returns false.

3. static byte parseByte(String str): This method returns the primitive byte value contained in the string argument str.

4. String toString(): The toString() method converts Byte object into String object and returns that String object.

5. static Byte valueOf(String str): It converts string str containing some byte value into Byte class object and returns that Byte object.

In other words, it returns a Byte object holding a value given by the specified String.


6. static Byte valueOf(byte b): This method converts the primitive byte value into Byte class object. In other words, it returns a Byte object representing the specified byte value.

7. static int compare(byte x, byte y): It is used to compare two byte values numerically.

8. static Byte decode(String str): This method decodes a String into a Byte.

9. static int toUnsignedInt(byte x): This method converts byte value to an int by an unsigned conversion.


10. static long toUnsignedLong(byte x): This method converts byte value to a long by an unsigned conversion.

Example Program based on Java Byte class


1. Let’s take an example program where we will compare the values of two Byte objects. We will accept data from the keyboard using InputStreamReader and BufferedReader classes. Look at the source code to understand better.

Program source code 1:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class ByteTest {
public static void main(String[] args) throws IOException 
{
// Create an object of InputStreamReader and BufferedReader class to accept data from keyboard.
  InputStreamReader isr = new InputStreamReader(System.in); 	
  BufferedReader br = new BufferedReader(isr);
  
// Take a byte value as string str1.
   System.out.println("Enter first byte number: ");
   String str1 = br.readLine();
   
// Create an object of Byte class and pass the str1 as a parameter to its constructor.
   Byte b1 = new Byte(str1);
	
// Take another byte value as string str2.
    System.out.println("Enter second byte number: ");
    String str2 = br.readLine();

// Create another object of Byte class and pass str2 as parameter to its constructor.
    Byte b2 = new Byte(str2);
   
// Now compare the content of both Byte objects.
    int num = b1.compareTo(b2);
   
if(num == 0)
  System.out.println("Contents of both Bytes are equal");
else
  if(num < 0)
    System.out.println(+b1 +" is less than " +b2);
else
    System.out.println(+b1 +" is greater than " +b2);
  }
}
Output:
      Enter first byte number: 
      20
      Enter second byte number: 
      30
      20 is less than 30

2. Let’s take another example program based on the above methods.

Program source code 2:

public class ByteTest {
public static void main(String[] args)
{
 String str1 = "10";
 String str2 = "20";
   
 byte num1 = Byte.parseByte(str1);
 System.out.println("parseByte(String str1) : " +num1);
 
 byte num2 = Byte.parseByte(str2);
 System.out.println("parseByte(String str2) : " +num2);
 
 Byte byte1 = Byte.decode(str1);
 System.out.println("decode(str1): " +byte1);
 
 Byte value1 = Byte.valueOf(str1);
 System.out.println("valueOf(str1): " +value1);
 
 int n1 = Byte.toUnsignedInt(num1);
 System.out.println("toUnsignedInt(num1): " +n1);
  }
}
Output:
       parseByte(String str1) : 10
       parseByte(String str2) : 20
       decode(str1): 10
       valueOf(str1): 10
       toUnsignedInt(num1): 10

3. Let’s take an example program where we will convert Byte class objects into String objects using toString() method and display them.

Program source code 3:

public class ByteTest {
public static void main(String[] args)
{
Byte b1, b2;
String s1, s2;
  b1 = new Byte("-123");
  b2 = new Byte("1");
    
  s1 = b1.toString(); // Converting Byte object into String object.
  s2 = b2.toString(); // Converting Byte object into String object.

// Now store String values into String variables and display them. 
  String str1 = s1;
  String str2 = s2;

System.out.println("First value in String form: " +str1);  
System.out.println("Second value in String form: " +str2);  
  }
}
Output:
      First value in String form: -123
      Second value in String form: 1

Hope that this tutorial has covered important points related to java Byte class with example programs. I hope that you will have understood this simple topic.
Thanks for reading!!!
Next ⇒ Java Short Class

⇐ Prev Next ⇒