Convert short to String in Java
Tags: String short Convert DecimalFormat
In this Java core tutorial, we learn how to convert short value into String value in Java via different solutions.
Using String.valueOf() method to create String from a short value
ConvertShortToStringExample1.java
public class ConvertShortToStringExample1 {
public static void main(String[] args) {
short value1 = 123;
String value2 = String.valueOf(value1);
System.out.println("short value: " + value1);
System.out.println("String value: " + value2);
}
}
short value: 123
String value: 123
Convert short to String using Short.toString() static method
ConvertShortToStringExample2.java
public class ConvertShortToStringExample2 {
public static void main(String[] args) {
short value1 = 456;
String value2 = Short.toString(value1);
System.out.println("short value: " + value1);
System.out.println("String value: " + value2);
}
}
short value: 456
String value: 456
Using Short.toString() method to get String value from a Short object
ConvertShortToStringExample3.java
public class ConvertShortToStringExample3 {
public static void main(String[] args) {
Short value1 = 789;
String value2 = value1.toString();
System.out.println("short value: " + value1);
System.out.println("String value: " + value2);
}
}
short value: 789
String value: 789
Format a short value to String using String.format() method
ConvertShortToStringExample4.java
public class ConvertShortToStringExample4 {
public static void main(String[] args) {
short value1 = 987;
String value2 = String.format("%d", value1);
System.out.println("short value: " + value1);
System.out.println("String value: " + value2);
}
}
short value: 987
String value: 987
Format a short value to String using DecimalFormat class
ConvertShortToStringExample5.java
import java.text.DecimalFormat;
public class ConvertShortToStringExample5 {
public static void main(String[] args) {
short value1 = 909;
DecimalFormat decimalFormat = new DecimalFormat("#");
String value2 = decimalFormat.format(value1);
System.out.println("short value: " + value1);
System.out.println("String value: " + value2);
}
}
short value: 909
String value: 909
Happy Coding 😊