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