The Byte
class in Java is a wrapper class for the primitive data type byte
. It provides several utility methods to work with byte
values. This class is part of the java.lang
package.
Key Features of Byte Wrapper Class
- Immutability: Instances of the
Byte
class are immutable. - Static Methods: The
Byte
class provides static methods for various operations, such as parsing and comparing bytes. - Byte Conversion: Methods to convert
byte
values to other types and vice versa.
Byte Class Methods
Here are some commonly used methods in the Byte
class:
byte byteValue()
int compareTo(Byte anotherByte)
static Byte decode(String nm)
boolean equals(Object obj)
float floatValue()
int hashCode()
int intValue()
long longValue()
static byte parseByte(String s)
static byte parseByte(String s, int radix)
short shortValue()
String toString()
static String toString(byte b)
static Byte valueOf(byte b)
static Byte valueOf(String s)
static Byte valueOf(String s, int radix)
1. byte byteValue()
Returns the value of this Byte
object as a byte
.
public class ByteExample {
public static void main(String[] args) {
Byte b = 10;
byte value = b.byteValue();
System.out.println(value); // Output: 10
}
}
Output:
10
2. int compareTo(Byte anotherByte)
Compares two Byte
objects numerically.
public class ByteExample {
public static void main(String[] args) {
Byte b1 = 10;
Byte b2 = 20;
System.out.println(b1.compareTo(b2)); // Output: -1
}
}
Output:
-10
3. static Byte decode(String nm)
Decodes a String
into a Byte
.
public class ByteExample {
public static void main(String[] args) {
Byte b = Byte.decode("10");
System.out.println(b); // Output: 10
}
}
Output:
10
4. boolean equals(Object obj)
Compares this object to the specified object.
public class ByteExample {
public static void main(String[] args) {
Byte b1 = 10;
Byte b2 = 10;
System.out.println(b1.equals(b2)); // Output: true
}
}
Output:
true
5. float floatValue()
Returns the value of this Byte
object as a float
.
public class ByteExample {
public static void main(String[] args) {
Byte b = 10;
float value = b.floatValue();
System.out.println(value); // Output: 10.0
}
}
Output:
10.0
6. int hashCode()
Returns a hash code for this Byte
object.
public class ByteExample {
public static void main(String[] args) {
Byte b = 10;
System.out.println(b.hashCode()); // Output: 10
}
}
Output:
10
7. int intValue()
Returns the value of this Byte
object as an int
.
public class ByteExample {
public static void main(String[] args) {
Byte b = 10;
int value = b.intValue();
System.out.println(value); // Output: 10
}
}
Output:
10
8. long longValue()
Returns the value of this Byte
object as a long
.
public class ByteExample {
public static void main(String[] args) {
Byte b = 10;
long value = b.longValue();
System.out.println(value); // Output: 10
}
}
Output:
10
9. static byte parseByte(String s)
Parses the string argument as a signed decimal byte
.
public class ByteExample {
public static void main(String[] args) {
byte b = Byte.parseByte("10");
System.out.println(b); // Output: 10
}
}
Output:
10
10. static byte parseByte(String s, int radix)
Parses the string argument as a signed byte
in the specified radix.
public class ByteExample {
public static void main(String[] args) {
byte b = Byte.parseByte("A", 16);
System.out.println(b); // Output: 10
}
}
Output:
10
11. short shortValue()
Returns the value of this Byte
object as a short
.
public class ByteExample {
public static void main(String[] args) {
Byte b = 10;
short value = b.shortValue();
System.out.println(value); // Output: 10
}
}
Output:
10
12. String toString()
Returns a String
object representing this Byte
's value.
public class ByteExample {
public static void main(String[] args) {
Byte b = 10;
System.out.println(b.toString()); // Output: 10
}
}
Output:
10
13. static String toString(byte b)
Returns a String
object representing the specified byte
.
public class ByteExample {
public static void main(String[] args) {
byte b = 10;
System.out.println(Byte.toString(b)); // Output: 10
}
}
Output:
10
14. static Byte valueOf(byte b)
Returns a Byte
instance representing the specified byte
value.
public class ByteExample {
public static void main(String[] args) {
Byte b = Byte.valueOf((byte) 10);
System.out.println(b); // Output: 10
}
}
Output:
10
15. static Byte valueOf(String s)
Returns a Byte
object holding the value given by the specified String
.
public class ByteExample {
public static void main(String[] args) {
Byte b = Byte.valueOf("10");
System.out.println(b); // Output: 10
}
}
Output:
10
16. static Byte valueOf(String s, int radix)
Returns a Byte
object holding the value extracted from the specified String
when parsed with the radix given by the second argument.
public class ByteExample {
public static void main(String[] args) {
Byte b = Byte.valueOf("A", 16);
System.out.println(b); // Output: 10
}
}
Output:
10
Conclusion
The Byte
class in Java provides a wide array of methods for manipulating and converting byte values. Understanding and utilizing these methods can greatly enhance your ability to handle byte operations in your applications.
Comments
Post a Comment
Leave Comment