Convert short to int in Java
Tags: int short Integer Convert
In this Java core tutorial, we learn how to convert short value into int value in Java via different solutions.
Table of contents
Assign short variable to int variable
We can convert shot value into in value by simply assign directly a short variable to an int variable as following Java example.
ConvertShortToIntExample1.java
public class ConvertShortToIntExample1 {
public static void main(String[] args) {
short value1 = 87;
int value2 = value1;
System.out.println("short value: " + value1);
System.out.println("int value: " + value2);
}
}
short value: 87
int value: 87
Using Integer.valueOf() method to instantiate int value from a short value
The following example Java program to show how to use the Integer.valueOf() method to convert a short value to an int value.
ConvertShortToIntExample2.java
public class ConvertShortToIntExample2 {
public static void main(String[] args) {
short value1 = 87;
int value2 = Integer.valueOf(value1);
System.out.println("short value: " + value1);
System.out.println("int value: " + value2);
}
}
short value: 87
int value: 87
Using Short.intValue() method to get int value from an Short object
Given an Short object we can use Short.intValue() method to get the int value as below Java example.
ConvertShortToIntExample3.java
public class ConvertShortToIntExample3 {
public static void main(String[] args) {
Short value1 = 57;
int value2 = value1.intValue();
System.out.println("short value: " + value1);
System.out.println("int value: " + value2);
}
}
short value: 57
int value: 57
Happy Coding 😊