java.lang.Byte Class

The Byte class wraps the value of primitive data type byte into Byte object. An object of type Byte contains a single field whose type is byte. In other words the wrapper classes create objects for primitive data types. The wrapper classes are Boolean, Byte, Character, Short, Integer, Float, Long and Double.

This class has following constructors.

Byte Class Constructors

Byte(byte value)

Constructs a newly allocated Byte object that represents the specified byte value.

Byte(String s)

Constructs a newly allocated Byte object that represents the byte value indicated by the String parameter.

java.lang.Byte Class Example
/* Save with file name ByteExample.java */

public class ByteExample
{
	public static void main(String args[])
	{
		//BYTE DECLARATION
		Byte b;
		//PRIMITIVE byte DECLARATION AND VALUE ASSIGNMENT
		byte a = 101;
		//MEMORY ALLOCATION FOR BYTE
		//DON'T USE BELOW CONSTRUCTOR LIKE THIS b = new Byte(101)
		//BECAUSE JAVA COMPILER UNDERSTANDS 101 AS AN int
		b = new Byte(a);
		//BYTE CLASS OUTPUT
		System.out.println();
		System.out.println("Byte Class Example");
		System.out.println("=====================");
		System.out.println();
		//RETURNS byte PRIMITIVE DATA TYPE
		System.out.println("Value is : "+ b.byteValue());
		//RETURNS double PRIMITIVE DATA TYPE
		System.out.println("double Value is : "+ b.doubleValue());
		//RETURNS float PRIMITIVE DATA TYPE
		System.out.println("float Value is : "+ b.floatValue());
		//RETURNS int PRIMITIVE DATA TYPE
		System.out.println("int Value is : "+ b.intValue());
		//RETURNS long PRIMITIVE DATA TYPE
		System.out.println("long Value is : "+ b.longValue());
		//RETURNS short PRIMITIVE DATA TYPE
		System.out.println("short Value is : "+ b.shortValue());
	}
}
                        
java.lang.Byte Class Example 2
/* Save with file name ByteExample2.java */

public class ByteExample2
{
	public static void main(String args[])
	{
		//BYTE DECLARATION
		Byte b, b2;
		//PRIMITIVE byte DECLARATION AND VALUE ASSIGNMENT
		byte a = 101;
		//MEMORY ALLOCATION FOR BYTE
		b = new Byte(a);
		b2 = new Byte("102");
		//BYTE CLASS OUTPUT
		System.out.println();
		System.out.println("Byte Class Example");
		System.out.println("=====================");
		System.out.println();
		System.out.println("b Value is : "+ b.byteValue());
		System.out.println("b2 Value is : "+ b2.byteValue());
		System.out.println();
		System.out.println("b compareTo b2 : "+ b.compareTo(b2));
		System.out.println("b equals b2 : "+ b.equals(b2));
	}
}
                        

In the following example you can learn how to use static methods of Byte Class. If you use static methods you need not to create the class instance. You can call static methods using the class name as reference.

java.lang.Byte Class Example 3
/* Save with file name ByteExample3.java */

public class ByteExample3
{
	public static void main(String args[])
	{
		//BYTE CLASS STATIC METHODS USAGE
		System.out.println("Byte Class Example");
		System.out.println("=====================");
		System.out.println();
		//PRIMITIVE byte DECLARATION AND VALUE ASSIGNMENT
		byte a = 101;
		//RETURN Byte OBJECT WITH THE SPECIFIED PRIMITIVE byte DATA TYPE
		System.out.println("valueOf : "+ Byte.valueOf(a));
		//RETURN Byte OBJECT WITH THE SPECIFIED
		//PRIMITIVE byte DATA TYPE AS String
		System.out.println("valueOf : "+ Byte.valueOf("103"));
		//RETURN PRIMITIVE byte DATA TYPE WITH THE SPECIFIED
		//PRIMITIVE byte DATA TYPE AS String
		System.out.println("parseByte : "+ Byte.parseByte("105"));
	}
}