In this article, we will discuss various ways to convert Short to String in Java
Short:
- Size is 2 bytes
- Its range is -32,768 to 32,767
Various ways to convert Short to String
- using Short.toString(shortVal);
- using String.valueOf(shortVal);
- create Short object and then convert to String using toString() method {new Short(shortVal).toString();}
- adding double quotes (“”) to short value {i.e.; “” + shortVal;}
- using append() method of StringBuffer or StringBuilder
Auto-boxing and un-boxing feature from Java 1.5 version:
- parseShort() method returns primitive short data-type but it can be easily used as Short wrapper-type
- as auto-boxing feature helps to convert primitive data-type to wrapper-types
- similarly, Short.valueOf() method returns Short wrapper-type but it can also be used as primitive short data-type
- let’s see one example on this auto-boxing feature at the very end
Read String class in detail with example
Let us move forward and discuss all possible ways to convert Short to String in Java
Way 1: Using Short.toString(shortVal); method
- This method can be used to convert primitive short data-type or Short wrapper-type to String object
- Negative short data-type can also be converted to String type, as shown in the below example
Method signature:
public static String toString(short s);
ConvertShortToStringUsingToStringMethod.java
package in.bench.resources.shorts.to.string.conversion; public class ConvertShortToStringUsingToStringMethod { public static void main(String[] args) { // primitive short data-type short shortValue1 = 30250; // 1. converting short to String // by using toString() method String str1 = Short.toString(shortValue1); System.out.println("1. Converted" + " primitive short to String value is : " + str1); // primitive short data-type (negative value) short shortValue2 = -20150; // 2. converting short to String // by using toString() method String str2 = Short.toString(shortValue2); System.out.println("\n2. Converted negative " + "primitive short to String value is : " + str2); // Short object Short iValue3 = new Short((short)12345); // 3. converting Short object to String // by using toString() method String str3 = Short.toString(iValue3); System.out.println("\n3. Converted" + " Short object to String value is : " + str3); } }
Output:
1. Converted primitive short to String value is : 30250 2. Converted negative primitive short to String value is : -20150 3. Converted Short object to String value is : 12345
Way 2: Using String.valueOf(shortVal); method
- This method can be used to convert primitive short data-type or Short wrapper-type to String object
- Negative short data-type can also be converted to String type, as shown in the below example
Method signature:
public static String valueOf(int i);
ConvertShortToStringUsingValueOfMethod.java
package in.bench.resources.shorts.to.string.conversion; public class ConvertShortToStringUsingValueOfMethod { public static void main(String[] args) { // primitive short data-type short shortValue1 = 11225; // 1. converting short to String // by using valueOf() method String str1 = String.valueOf(shortValue1); System.out.println("1. Converted" + " primitive short to String value is : " + str1); // primitive short data-type (negative value) short shortValue2 = -10296; // 2. converting negative short to String // by using valueOf() method String str2 = String.valueOf(shortValue2); System.out.println("\n2. Converted " + "negative primitive short to String value is : " + str2); // Short object Short shortValue3 = new Short((short)12345); // 3. converting Short object to String // by using valueOf() method String str3 = String.valueOf(shortValue3); System.out.println("\n3. Converted" + " Short object to String value is : " + str3); } }
Output:
1. Converted primitive short to String value is : 11225 2. Converted negative primitive short to String value is : -10296 3. Converted Short object to String value is : 12345
Way 3: create Short object and then convert to String using toString() method {new Short(shortVal).toString();}
- Here, we will create new Short object with primitive short value as constructor-argument
- After creating new Short object by passing primitive short value, then invoke toString() method for converting primitive short data-type to String object
Method signature:
public Short(short value); public String toString();
ConvertShortToStringUsingObjectCreation.java
package in.bench.resources.shorts.to.string.conversion; public class ConvertShortToStringUsingObjectCreation { public static void main(String[] args) { // primitive short data-type short shortValue1 = 5698; // 1. converting short to String // by creating Short object Short shortObj1 = new Short(shortValue1); // and then invoke toString(); method String str1 = shortObj1.toString(); System.out.println("1. Converted" + " primitive short to String value is : " + str1); // primitive short data-type (negative value) short shortValue2 = -10236; // 2. converting negative short to String // by creating Short object Short shortObj2 = new Short(shortValue2); // and then invoke toString(); method String str2 = shortObj2.toString(); System.out.println("\n2. Converted " + "negative primitive short to String value is : " + str2); // Short object Short shortValue3 = new Short((short)14785); // 3. converting Short object to String // by creating Short object String str3 = shortValue3.toString(); System.out.println("\n3. Converted" + " Short object to String value is : " + str3); } }
Output:
1. Converted primitive short to String value is : 5698 2. Converted negative primitive short to String value is : -10236 3. Converted Short object to String value is : 14785
Way 4: adding double quotes (“”) to short value
- Note: for this conversion to work, double-quotes (“”) must be added at the start of the concatenation
Syntax:
String temp = “” + shortValue;
ConvertShortToStringByAddingDoubleQuotes.java
package in.bench.resources.shorts.to.string.conversion; public class ConvertShortToStringByAddingDoubleQuotes { public static void main(String[] args) { // primitive short data-type short shortValue1 = 30201; // 1. converting short to String by adding "" String str1 = "" + shortValue1; System.out.println("1. Converted" + " primitive short to String value is : " + str1); // primitive short data-type (negative value) short shortValue2 = -10203; // 2. converting negative short to String by adding "" String str2 = "" + shortValue2; System.out.println("\n2. Converted " + "negative primitive short to String value is : " + str2); // Short object Short shortValue3 = new Short((short)31214); // 3. converting Short object to String by adding "" String str3 = "" + shortValue3; System.out.println("\n3. Converted" + " Short object to String value is : " + str3); } }
Output:
1. Converted primitive short to String value is : 30201 2. Converted negative primitive short to String value is : -10203 3. Converted Short object to String value is : 31214
Way 5: Using append() method of StringBuffer & StringBuilder
- This method can be used to convert primitive short data-type to String object
- Only sign is allowed, like minus (-)
Syntax:
StringBuffer sbf = sbf.append(shortValue); String temp = sbf.toString();
ConvertShortToStringByAppending.java
package in.bench.resources.shorts.to.string.conversion; public class ConvertShortToStringByAppending { public static void main(String[] args) { // primitive short data-type short shortValue1 = 15489; // 1. converting short to String // by using append() method StringBuffer sbf1 = new StringBuffer(); String str1 = sbf1.append(shortValue1).toString(); System.out.println("1. Converted" + " primitive short to String value is : " + str1); // primitive short data-type (negative value) short shortValue2 = -25412; // 2. converting negative short to String // by using append() method StringBuffer sbf2 = new StringBuffer(); String str2 = sbf2.append(shortValue2).toString(); System.out.println("\n2. Converted " + "negative primitive short to String value is : " + str2); // Short object Short shortValue3 = new Short((short)1000); // 3. converting Short object to String // by using append() method StringBuffer sbf3 = new StringBuffer(); String str3 = sbf3.append(shortValue3).toString(); System.out.println("\n3. Converted" + " Short object to String value is : " + str3); } }
Output:
1. Converted primitive short to String value is : 15489 2. Converted negative primitive short to String value is : -25412 3. Converted Short object to String value is : 1000
What if we want to convert primitive short data-type to Short wrapper-type or vice-versa ?
- Auto-boxing feature available from Java 1.5 version
- So, converting primitive data-type to wrapper-type can easily be done, by directly assigning
- Let’s see one example based on this auto-boxing feature
AutoBoxingFeatureForShortConversion.java
package in.bench.resources.string.to.shorts.conversion; public class AutoBoxingFeatureForShortConversion { public static void main(String[] args) { // String with only digits String str1 = "32120"; // string to short conversion short shortValue1 = Short.parseShort(str1); // 1. Auto-Boxing - converting short to Short Short shortAutoBoxing = shortValue1; System.out.println("1. Auto-Boxing : " + shortAutoBoxing); // String with only digits and a minus sign (-) String str2 = "-15936"; // string to Short conversion Short shortValue2 = Short.valueOf(str2); // 1. Un-Boxing - converting Short to short short shortUnBoxing = shortValue2; System.out.println("\n2. Un-Boxing : " + shortUnBoxing); } }
Output:
1. Auto-Boxing : 32120 2. Un-Boxing : -15936
Hope, you found this article very helpful. If you have any suggestion or want to contribute any other way or tricky situation you faced during Interview hours, then share with us. We will include that code here.
References:
- https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
- https://docs.oracle.com/javase/tutorial/java/data/numberclasses.html
- https://docs.oracle.com/javase/tutorial/java/data/autoboxing.html
- https://docs.oracle.com/javase/tutorial/java/data/converting.html
- https://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html
- https://docs.oracle.com/javase/6/docs/api/java/lang/Short.html
- https://docs.oracle.com/javase/7/docs/api/java/lang/Short.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/Short.html
- https://docs.oracle.com/javase/tutorial/java/data/strings.html
- https://docs.oracle.com/javase/6/docs/api/java/lang/String.html
- https://docs.oracle.com/javase/7/docs/api/java/lang/String.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/String.html
- https://docs.oracle.com/javase/6/docs/api/java/lang/class-use/String.html
- https://docs.oracle.com/javase/7/docs/api/java/lang/class-use/String.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/class-use/String.html
- https://docs.oracle.com/javase/tutorial/essential/concurrency/immutable.html
- https://docs.oracle.com/javase/tutorial/essential/concurrency/imstrat.html
- http://docs.oracle.com/javase/7/docs/api/java/lang/NumberFormatException.html
Happy Coding !!
Happy Learning !!