Updates
  • Starting New Weekday Batch for Java Full Stack Developer on 24th June 2024 @ 02:00 PM to 04:00 PM
  • Starting New Weekend Batch for Java Full Stack Developer on 06th July 2024 @ 11:00 AM to 02:00 PM
  • Starting New Weekday Batch for Java Full Stack Developer on 08th July 2024 @ 04:00 PM to 06:00 PM
  • Starting New Weekday Batch for Java Full Stack Developer on 29th July 2024 @ 10:00 AM to 12:00 PM
Join Course

java.lang.Byte

• Byte is a built-in class in Java available in java.lang package.
• It is a Wrapper class for byte primitive data type.
• It is a final type of class so no otherne class can inherit java.lang.Byte class and we cannot override itstheir methods.
• java.lang.Byte class objects are always will be an Iimmutable object.,
• Therefore, it meant wwhen we perform any operation on the objects of java.lang.Byte class- such as add, sub and etc.- then along with the existing objects of javaof java.lang.Byte class always we will always get new objects thatwhich contains the results.

            
class A{
	public static void main(String arg[]){
		Byte b1 = new Byte("10");
		Byte b2 = new Byte("20");
		
		Byte b3 = (byte)(b1 + b2);
		
		System.out.println("b1 == b3 :- "+(b1 == b3)); // false
		System.out.println("b2 == b3 :- "+(b2 == b3)); // false

	}
}
    b1 == b3 :- false
    b2 == b3 :- false
            
• java.lang.Byte class: -

public final class java.lang.Byte extends java.lang.Number implements java.lang.Comparable<java.lang.Byte> {
public static final byte MIN_VALUE;
public static final byte MAX_VALUE;
public static final java.lang.Class<java.lang.Byte> TYPE;
public static final int SIZE;
public static final int BYTES;
public static java.lang.String toString(byte);
public static java.lang.Byte valueOf(byte);
public static byte parseByte(java.lang.String, int) throws java.lang.NumberFormatException;
public static byte parseByte(java.lang.String) throws java.lang.NumberFormatException;
public static java.lang.Byte valueOf(java.lang.String, int) throws java.lang.NumberFormatException;
public static java.lang.Byte valueOf(java.lang.String) throws java.lang.NumberFormatException;
public static java.lang.Byte decode(java.lang.String) throws java.lang.NumberFormatException;
public java.lang.Byte(byte);
public java.lang.Byte(java.lang.String) throws java.lang.NumberFormatException;
public byte byteValue();
public short shortValue();
public int intValue();
public long longValue();
public float floatValue();
public double doubleValue();
public java.lang.String toString();
public int hashCode();
public static int hashCode(byte);
public boolean equals(java.lang.Object);
public int compareTo(java.lang.Byte);
public static int compare(byte, byte);
public static int toUnsignedInt(byte);
public static long toUnsignedLong(byte);
public int compareTo(java.lang.Object);
static {};
}

As we can see in the above API of java.lang.Byte class, we have different utility methods or variables to perform different operations along with byte type of values.

Here we will discuss about some important members of java.lang.Byte class: -

1. public static final byte MIN_VALUE;

Access Modifier: - public
Member Type: - static final variable
Data Type: - byte
variable Name: - MIN_VALUE
Functionality: - The method is used for storing the minimum value of -128 which we can store into the byte type of variable and the same minimum value we can pass as an argument in the java.lang.Byte class constructor to create an object.

When we try to store a value lesser than the minimum value of -128, then we get Incompatible type error at compile-time. If we pass into a constructor a value lesser than lower limit value for creating an object of java.lang.Byte class then we will get an exception at runtime that will appears as java.lang.NumberFormatException.

            
class JTC{
	public static void main(String arg[]){
	byte b1 = -128; 
	// byte b2 = -129;  --> incompatible types
	Byte b3 = new Byte("-128");
// Byte b4 = new Byte("-129"); --> java.lang.NumberFormatException
	}
}

In the above example we can see that in JTC class first we have declared a byte type of variable which is byte b1 and thereafter we are assigning a value of -128. Since the value of -128 is in the range of byte primitive data type, hence it is valid.
In the next line we have one more variable byte b2 = -129 ; however -129 is not in the range of byte, so here we get incompatibility error at compile time.
Then after we are creating an object of java.lang.Byte class (Byte b3 = new Byte (“-128”)) . We can see that we are passing -128 as an argument into the java.lang.Byte class constructor. Since the value of -128 is in the range of byte as String, hence it is valid.
Finally in the last section of the above example we are creating an object of java.lang.Byte class along with -129 argument. Since -129 is not in the range of byte hence here we will not get any compile time error; however we get java.lang.NumberFormatException at runtime, which shows that the statement is invalid.

2. public static final byte MAX_VALUE;

Access Modifier: - public
Member Type: - static final variable
Data Type: - byte
Variable Name: - MAX_VALUE
Functionality: - It stores the maximum value (127) which we can store into the byte type of variable and the same value we can pass as an argument into the java.lang.Byte class constructor for creating an object.
When we try to store a value which is greater than the maximum value (127) then we get Incompatible type error at compile-time.
For creating an object of java.lang.Byte class if we pass into the constructor an argument of value greater than the upper limit then we will get an exception at run time which will appear as java.lang.NumberFormatException.

            
class JTC{
	public static void main(String arg[]){
byte b1 = 127; 
// byte b2 = 128;  --> incompatible types
Byte b3 = new Byte("127");
// Byte b4 = new Byte("128"); --> java.lang.NumberFormatException
	}
}

In the above example we can see that in JTC class first we have declared a byte type of variable i.e., byte b1, and have assigned it a value of 127. Since 127 is in the range of byte primitive data type, hence it is a valid value.
In the next line we have one more variable i.e., byte b2 = 128; however, as we have discussed, 128 is not in the range of byte; so here we get incompatibility error at compile time.
Then after we are creating an object of java.lang.Byte class (Byte b3 = new Byte (“127”)) and we can see that we are passing 127 as an argument into the java.lang.Byte class constructor. Since 127 is in the range of byte as String, hence it is valid.
Finally in the last section of the above example we are creating an object of java.lang.Byte class along with 129 as an argument. Since129 is not in the range of byte so here we will not get any compile time error; however we will get java.lang.NumberFormatException at runtime that shows that the statement is invalid.

3. public static byte parseByte(java.lang.String) :-

Access Modifier: - public
Member Type: - static Method
Return Type: - byte
Method Name: - parseByte
Parameter: - java.lang.String
Functionality: - As we know that we cannot typecast a String type of value into the byte type, but sometimes as per our business logic we convert the String literals into byte type using parseByte() method. When we use parseByte() then we should commit into our memory two important aspects; one is that the argument of parseByte(String) can contain only numeric value and secondly the numeric value must be in the range of byte which is -128 to 127, otherwise we get an exception java.lang.NumberFormatException.

            
class JTC{
	public static void main(String arg[]){
		String s1 = "127";
		byte b1 = Byte.parseByte(s1);
		System.out.println("b1 :- "+b1);

		String s2 = "128";
		// byte b2 = Byte.parseByte(s2); --> java.lang.NumberFormatException

		String s3 = "abc";
		// byte b3 = Byte.parseByte(s3); --> java.lang.NumberFormatException
	}

}
    b1 :- 127
            

In the above example we can see that in the first invocation of parseByte() method we are passing “127” as argument and because argument is in number format and 127 is in the range of byte so this statement is valid.
In the next two invocations of parseByte() method we are passing “128” and “abc” String argument respectively and as we have discussed- “128” is not in the range of byte and “abc” is not in number format- so we get java.lang.NumberFromatException for both the invocations at the time of execution of the program.

4. public int hashCode(): -

Access Modifier: - public
Member Type: - Instance Method
Return Type: - int
Method Name: - hashCode()
Parameter: - No parameter
Functionality: - hashCode() method is overridden from java.lang.Object class into java.lang.Byte class. In java.lang.Byte class the method returns same value that we have pass as an argument into the Byte class constructor for creating current working object of java.lang.Byte class.

            
class JTC{
	public static void main(String arg[]){
		Byte b1 = new Byte("10");
		System.out.println(b1.hashCode());
	}
}
    10
            

In the above example we are creating an object of java.lang.Byte class and passing “10” as an argument (Byte b1 = new Byte (“10”)) and in the next line we are invoking hashCode() along with b1. As we have discussed earlier that hashCode() method gets overridden in java.lang.Byte class and that is the reason we are getting the output as 10 here.

5. public boolean equals(Object obj): -

Access Modifier: - public
Member Type: - Instance Method
Return Type: - boolean
Method Name: - equals()
Parameter:- Object
Functionality: - equals() method is overridden in java.lang.Byte class, and here the method compares an object of the class to the specified object. The result is true if and only if the argument is not null and is a Byte class object that contains the same byte value as the specified object.

            
class JTC{
	public static void main(String arg[]){
		Byte b1 = new Byte("10");
		Byte b2 = new Byte("20");
		
		System.out.println("b1.equals(b2) :- "+(b1.equals(b2))); // false

		Byte b3 = new Byte("10");
		
		System.out.println("b1.equlas(b3) :- " + (b1.equals(b3))); // true

	}
}  
    b1.equals(b2) :- false
    b1.equlas(b3) :- true
            

In this example we are creating two different objects of Byte class that are Byte b1 = new Byte (“10”) and Byte b2 = new Byte (“20”), and because we are passing two different values to create objects of java.lang.Byte class so here we are getting false.
In the next line again, we are creating an object of Byte class (Byte b3 = new Byte (“10”)) and then after we are comparing b1 and b3 using equals () method, because both objects contain the same value so we will get true as the result.