EN
Java - convert String to byte
12 points
In Java, we can convert String to byte in couple of different ways.
Short solutions:
xxxxxxxxxx
1
// solution 1
2
byte num1 = Byte.parseByte("31");
3
4
// solution 2
5
Byte num2 = Byte.valueOf("31");
6
7
// solution 3
8
Byte num3 = new Byte("31");
xxxxxxxxxx
1
public class Example1 {
2
3
public static void main(String[] args) {
4
String str = "31";
5
byte num = Byte.parseByte(str);
6
System.out.println(num); // 31
7
}
8
}
Output:
xxxxxxxxxx
1
31
xxxxxxxxxx
1
public class Example2 {
2
3
public static void main(String[] args) {
4
String str = "31";
5
Byte num = Byte.valueOf(str);
6
System.out.println(num); // 31
7
}
8
}
Output:
xxxxxxxxxx
1
31
xxxxxxxxxx
1
public class Example3 {
2
3
public static void main(String[] args) {
4
String str = "31";
5
Byte num = new Byte(str);
6
System.out.println(num); // 31
7
}
8
}
Output:
xxxxxxxxxx
1
31