Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
In this blog, we shall look into the Byte Class of Java. It is also called wrapper class in Java, and this name is given to it because of its work of wrapping the primitive data type (generally byte type) to its object. Consider this article to learn more about Data type. It also provides us with a constructor to create an object.
In this article, we will get ourselves acquainted with several static methods of the Byte class of Java. If you are not familiar with the static keyword, consider reading this blog first.
To use any of the features of this class, first, you need to import its package java.lang.Byte package. In Java's Character class, there is only one field of type char present in the class, and also, there is only one constructor to initialize that field. How to use the constructor to initialize the field is shown below.
Byte b1 = new Byte(byte b);
It creates a Byte object ‘b1’ initialized with the value ‘b’.
Byte b1 = Byte(String b);
It creates a Byte object initialized with the value provided in string representation which is actually byte. Default radix is 10. It also gives NumbeFormatException if the provided value is out of bounds of the byte.
static int BYTES: Represents the number of bytes used to show byte value in two’s complement binary form.
static byte MAX_VALUE: It's a constant field representing the maximum byte value i.e. 127.
static byte MIN_VALUE: It's a constant field representing the minimum byte value i.e. -128.
static int SIZE: Represents the number of bits used to show byte value in two’s complement binary form.
static Class<Byte> TYPE: It is the class instance of type byte.
Methods of Byte Class
Methods of this class are:-
S.No.
Methods
Description
1.
byteValue()
returns value of this Byte as a byte
2.
compare()
Compares the two specified byte values, not the Integer compare method.
3.
compareTo()
Compares two Byte objects numerically
4.
compareUnsigned()
Compares the two unsigned byte values
5.
decode()
Use to decode a String into a Byte
6.
doubleValue()
returns double value for this Byte object
7.
equals()
Use to compare this object against the specified byte object
8.
floatValue()
returns value of this Byte as a Float
9.
hashCode()
returns hash code for this Byte object
10.
intValue()
returns int value for this Byte object
11.
longValue()
returns long value for this Byte object
12.
parseByte()
Use to parse the string argument as a signed decimal byte.
13.
shortValue()
returns short value for this Byte object
14.
toString()
returns string representation of the Byte object
15.
toUnsignedInt()
Use to convert the specified byte to an int by an unsigned conversion
16.
toUnsignedLong()
Use to convert the specified byte to a long by an unsigned conversion
17.
valueOf()
Returns a Byte instance representing the given byte or string value.
Examples
Now, we will discuss some of the methods from the above table with examples.
1. byte byteValue()
Syntax:
byte byteValue()
Code:
public class Demo {
public static void main(String[] args) {
//testing the byteValue method.
//storing the minimum value to the val1
Byte val1=Byte.MIN_VALUE;
System.out.println(val1.byteValue());
//storing the maximum value to val2.
Byte val2=Byte.MAX_VALUE;
System.out.println(val2.byteValue());
}
}
Output:
-128
127
2. int byteCompare(byte val1, byte val2)
Syntax:
int byteCompare(byte val1, byte val2)
Code:
//This function actually return the difference (val1-val2) which can be also use to //compare, not like Integer’s class compare method
public class Demo {
public static void main(String[] args) {
byte x=34;
byte y=67;
int diff1=Byte.compare(x,y);
int diff2=Byte.compare(y,x);
System.out.println(diff1);
System.out.println(diff2);
//It can also used to compare two byte
if(Byte.compare(y,x) > 0)
System.out.println(y+” is greater.”);
else
System.out.println(x+” is greater.”);
}
}
public class Demo {
public static void main(String[] args) {
Byte x=100;
byte y=Byte.MIN_VALUE;
int val=x.compareTo(y);
//comparing x and y using compareTo method.
if(val > 0)
System.out.println(x+" is greater than "+y);
else
System.out.println(y+" is greater than "+x);
}
}
public class Demo {
public static void main(String[] args) {
String s="100";
//return the byte value of string s.
Byte val=Byte.decode(s);
System.out.println("Decoded value of s is: "+val);
}
}
Output:
Decoded value of s is: 100
5. double doubleValue()
Syntax:
double doubleValue()
Code:
public class Demo {
public static void main(String[] args) {
Byte x=102;
//converting the byte value to double.
Double x_db = x.doubleValue();
System.out.println("Double converted value of x: "+x_db);
}
}
Output:
Double converted value of x: 102.0
6. boolean equals(Object ob)
Syntax:
boolean equals(Object ob)
Code:
public class Demo {
public static void main(String[] args) {
Byte x=100;
Byte y=10;
Byte z=100;
//tells whether value are equal or not.
boolean res1=x.equals(y);
boolean res2=x.equals(z);
System.out.println("Is x and y are equal: "+res1);
System.out.println("Is x and z are equal: "+res2);
}
}
Output:
Is x and y are equal: false
Is x and z are equal: true
7. String toString(byte b)
Syntax:
String toString(byte b)
Code:
public class Demo {
public static void main(String[] args) {
Byte x=98;
String str=x.toString();
//printing the data type of str.
System.out.println("Type of str is: "+str.getClass().getSimpleName());
}
}
Byte valueOf(String str, int radix) throws NumberFormatException
Code:
public class Demo {
//testing the first type of method
public static void main(String[] args) {
Byte x=100;
System.out.println("Value Of method returns: "+Byte.valueOf(x));
}
}
Output:
Value Of method returns: 100
Code:
//Example of method overloading.
public class Demo {
public static void main(String[] args) {
String s="100";
System.out.println("Value Of method returns: "+Byte.valueOf(s));
}
}
Output:
Value Of method returns: 100
Code:
//Function Overloading
public class Demo {
public static void main(String[] args) {
String str="40";
int radix=12;
System.out.println("Value of method returns: "+Byte.valueOf(str,radix));
}
}
Output:
Value of method returns: 48
Frequently Asked Questions
What is the meaning of wrapper class? A wrapper class is a type that wraps or contains primitive data types in its object. When we construct an object for a wrapper class, it includes a field where we can store primitive data types. In other words, a primitive value can be wrapped into a wrapper class object.
What is the meaning of static methods? A static method is part of the specifications of the class, not the objects it creates. A static method, unlike instance methods, is referred by the class name and can be called without generating a class object.
Why does Byte compare method different from the Integer compare method? Subtraction is not used in the implementation of Integer.compare since it could cause an overflow if you're comparing huge integer values. This won't happen in the case of the byte.
Conclusion
In this article, we've thoroughly discussed the Byte class in Java and its implementation. We discussed how its constructors, fields, and methodscould be used in Java.
We hope that this blog has helped you enhance your knowledge regarding the Byte class in Java. We can use the concepts of Java in building an android app to learn in-depth about android development. Check out our Android Development course on the Coding Ninjas website. Do upvote our blog to help other ninjas grow.