The Byte class wraps a value of primitive type byte in an object. An object of type Byte contains a single field whose type is byte.
Following are some of the methods of the Byte class −
Sr.No. | Method & Description |
---|---|
1 | byte byteValue() This method returns the value of this Byte as a byte. |
2 | int compareTo(Byte anotherByte) This method compares two Byte objects numerically. |
3 | static Byte decode(String nm) This method decodes a String into a Byte. |
4 | double doubleValue() This method returns the value of this Byte as a double. |
5 | boolean equals(Object obj) This method compares this object to the specified object. |
6 | float floatValue() This method returns the value of this Byte as a float. |
7 | int hashCode() This method returns a hash code for this Byte. |
8 | int intValue() This method returns the value of this Byte as an int. |
9 | long longValue() This method returns the value of this Byte as a long. |
10 | static byte parseByte(String s) This method parses the string argument as a signed decimal byte. |
Let us now see an example−
Example
import java.lang.*; public class Demo { public static void main(String[] args){ Byte b1, b2; int i1, i2; b1 = new Byte("1"); b2 = new Byte("-1"); i1 = b1.intValue(); i2 = b2.intValue(); String str1 = "int value of Byte " + b1 + " is " + i1; String str2 = "int value of Byte " + b2 + " is " + i2; System.out.println( str1 ); System.out.println( str2 ); } }
Output
int value of Byte 1 is 1 int value of Byte -1 is -1
Example
Let us now see another example −
import java.lang.*; public class Demo { public static void main(String[] args){ Byte b1, b2; String s1, s2; b1 = new Byte("-123"); b2 = new Byte("0"); s1 = b1.toString(); s2 = b2.toString(); String str1 = "String value of Byte " + b1 + " is " + s1; String str2 = "String value of Byte " + b2 + " is " + s2; System.out.println( str1 ); System.out.println( str2 ); } }
Output
String value of Byte -123 is -123 String value of Byte 0 is 0