New Tutorials:   JAVASCRIPT    SASS/SCSS    PL/SQL  
See the Tutorial List

Java Byte Class

The Byte class is a wrapper class which is used to wraps a value of primitive type byte in an object. An object of type Byte contains a single field whose type is byte.

This class is located into java.lang package and provides several methods for converting a byte to a String and vice versa. The declaration of this class is given below.

Declaration:

public final class Byte extends Number implements Comparable<Byte>

Now lets see its methods and their usage with the help of examples.

1. toString()

Syntax:

	 
public String toString(byte b)
	 

Example:

Lets take an example to get string object of a byte type. We used toString() method which is static so we can call it by using the class name.

	
public class ByteDemo1 
{ 
	public static void main(String[] args)  
	{ 
		byte a = 65; 
		System.out.println("toString(a) = " + Byte.toString(a));
	}
}        
	

byte-class

2. valueOf()

This method returns a Byte instance representing the specified byte value. This method should generally be used in preference to the constructor Byte(byte).

Syntax:

	
public static Byte valueOf(byte b)
	

Example:

In this example, we are using valueOf() method that returns instance of Byte class which represents the specified byte type.

	
public class ByteDemo1 
{ 
    public static void main(String[] args)  
        { 
        byte a = 65; 
        String b = "25";
        Byte x = Byte.valueOf(a); 
        System.out.println("ValueOf(a) = " + x);
        x = Byte.valueOf(b, 6); 
        System.out.println("ValueOf(a,6) = " + x);
     }
}
	

byte-class-example

3. parseByte()

This method returns a byte value of the specified string val. We can use it to get a byte value from string type value.

	
public static byte parseByte(String val, int radix) throws NumberFormatException
	

Example:

Lets take an example in which we have a string type variable and getting its byte value using the parseByte() method.

	
public class ByteDemo1 
{ 
	public static void main(String[] args)  
	{ 
		byte a = 65; 
		String b = "25";
		byte x = Byte.parseByte(b); 
		System.out.println("parseByte(b) = " + x); 
		x = Byte.parseByte(b, 6); 
		System.out.println("parseByte(b,6) = " + x); 
	}
}
	

byte-class-example

4. decode()

This method is used to decode a String into a Byte. It accepts decimal, hexadecimal, and octal numbers.

Syntax:

	
public static Byte decode(String s) throws NumberFormatException
	

Example:

We can use decode method to decode string type to a byte object. See the below example.

	
public class ByteDemo1 
{ 
	public static void main(String[] args)  
	{ 
		String a = "25"; 
		String b = "006"; 
		String c = "0x0f"; 
		Byte x = Byte.decode(a); 
		System.out.println("decode(25) = " + x); 
		x = Byte.decode(b); 
		System.out.println("decode(006) = " + x); 
		x= Byte.decode(c); 
		System.out.println("decode(0x0f) = " + x); 
	}
}    
	

byte-class-example

5. byteValue()

This method is used to get a primitive type byte value from Byte object. It returns the numeric value represented by this object after conversion to type byte.

Syntax

	
public byte byteValue()
	

6. shortValue()

This method returns the value of this Byte as a short after a widening primitive conversion.

Syntax

	
public short shortValue()
	

7. intValue()

The intValue() method returns the value of this Byte as a primitive int type after a widening primitive conversion.

Syntax

	
public int intValue()
	

8. longValue()

The longValue() method returns the value of this Byte type as a long type after a widening primitive conversion.

Syntax

	
public long longValue()
	

9. doubleValue()

It returns the value of this Byte type as a double type after a widening primitive conversion.

Syntax

	
public double doubleValue()
	

10. floatValue()

This method is used to get value of this Byte type as a float type after a widening primitive conversion.

Syntax:

	
public float floatValue()
	

Example:

Lets take an example to convert byte type to int, long and float type values. In this example, we are using intValue(), floatValue(), doubleValue() methods.

	
public class ByteDemo1 
{ 
	public static void main(String[] args)  
	{ 
		Byte a = 45;
		Byte obj = new Byte(a);

		System.out.println("bytevalue(obj) = " + obj.byteValue()); 
		System.out.println("shortvalue(obj) = " + obj.shortValue()); 
		System.out.println("intvalue(obj) = " + obj.intValue()); 
		System.out.println("longvalue(obj) = " + obj.longValue()); 
		System.out.println("doublevalue(obj) = " + obj.doubleValue()); 
		System.out.println("floatvalue(obj) = " + obj.floatValue());
	}
}
	

byte-class-example

11. hashCode()

This method is used to get hash code of a byte value. It returns an int value of byte object.

Syntax:

	
public inthashCode()
	

Example:

	
public class ByteDemo1 
{ 
    public static void main(String[] args)  
	{ 
        Byte a = 45;
		Byte obj = new Byte(a);
	
		int x =obj.hashCode(); 
		System.out.println("hashcode(obj) = " + x); 
	}
}
	

byte-class-example

12. equals()

The equals() method compares an object to the specified object. It returns true if objects are same; false otherwise.

Syntax:

	
public boolean equals(Object obj) 
	

Example:

We are comparing two byte objects using the equals method that returns true if both the objects are true.

	
public class ByteDemo1 
{ 
    public static void main(String[] args)  
	{ 
        Byte a = 45;
		String b ="25";
		Byte obj = new Byte(a);
		Byte obj1 = new Byte(b);
		
		boolean z = obj.equals(obj1); 
		System.out.println("obj.equals(obj1) = " + z); 
	}
}
	

byte-class-example

13. compareTo()

This method is used to compare two byte objects numerically. It returns 0, if the both byte objects are equal. It returns less the 0, if one byte object is less than argument object. It returns greater than 0, if one byte object is numerically greater that the argument byte object.

Syntax:

public int compareTo(Byte anotherByte)

Example:

In this example, we are comparing two bytes objects using compareTo() method that compares two byte objects numerically and returns a numeric value.

	
public class ByteDemo1 
{ 
    public static void main(String[] args)  
	{ 
        Byte a = 45;
		String b ="25";
		Byte obj = new Byte(a);
		Byte obj1 = new Byte(b);
		
		int z = obj.compareTo(obj1); 
		System.out.println("obj.compareTo(obj1) = " + z); 
	}
}
	

byte-class-example

14. compare()

It is used to compare two byte values numerically. The value returned is identical to what would be returned by.

Syntax:

	
public static intcompare(byte x,byte y) 
	

Example:

We can use compare method to compare two byte values. It returns 0 if both are equal else returns either negative or positive numerical value.

	
public class ByteDemo1 
{ 
    public static void main(String[] args)  
	{ 
        Byte a = 45;
		String b ="25";
		Byte obj = new Byte(a);
		Byte obj1 = new Byte(b);
		
		int z = Byte.compare(obj, obj1); 
		System.out.println("compare(obj, obj1) = " + z); 
	}
}
	

byte-class-example