Convert String to short in Java
In this Java core tutorial, we learn how to convert String value into short value in Java via different solutions.
Parse String to short value using Short.parseShort() method
ConvertStringToShortExample1.java
public class ConvertStringToShortExample1 {
public static void main(String[] args) {
String value1 = "1234";
short value2 = Short.parseShort(value1);
System.out.println("String value: " + value1);
System.out.println("short value: " + value2);
}
}
String value: 1234
short value: 1234
Using Short.valueOf() method to instantiate short value from a given String
ConvertStringToShortExample2.java
public class ConvertStringToShortExample2 {
public static void main(String[] args) {
String value1 = "600";
short value2 = Short.valueOf(value1);
System.out.println("String value: " + value1);
System.out.println("short value: " + value2);
}
}
String value: 600
short value: 600
Happy Coding 😊