Byte class defines static methods to convert string value to byte value
static Byte valueOf(byte b)
- Returns a Byte instance representing the specified byte value.
static Byte valueOf(String s)
- Returns a Byte object holding the value given by the specified String.
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.
static byte parseByte(String s)
- Parses the string argument as a signed decimal byte.
public class Main {
public static void main(String[] args) {
System.out.println("parse string to byte:"+Byte.parseByte("10"));
}
}
The output:
parse string to byte:10
static byte parseByte(String s, int radix)
- Parses the string argument as a signed byte in the radix specified by the second argument.
In the following code we convert string 10 based on radix 8.
public class Main {
public static void main(String[] args) {
System.out.println("parse string to byte:"+Byte.parseByte("10", 8));
}
}
The output:
parse string to byte:8
And we use the valueOf methods in the same way:
public class Main {
public static void main(String[] args) {
System.out.println("parse string to byte:"+Byte.valueOf("10"));
System.out.println("parse string to byte:"+Byte.valueOf("10", 8));
}
}
The output:
parse string to byte:10
parse string to byte:8
Home
Java Book
Essential Classes
Java Book
Essential Classes
Byte:
- Byte
- Find out byte's max value, min value and size
- Create Byte object with its constructor
- Convert Byte to byte, double, float, int, long and short
- Decode a string to a byte
- Byte class defines static methods to convert string value to byte value
- Convert byte value to string value
- Compare two byte values