Skip to content

Java convert short to String example

This example shows how to convert short to String in Java using the toString, valueOf, and + operator. It also shows the best way to convert short to string.

How to convert short to String in Java?

There are several ways in which primitive short can be converted to a string in Java.

1) Convert short to String using the Short wrapper class

Use the toString method of the Short wrapper class to convert.

This method returns a string representation of the short value passed as a parameter.

Output

2) Using string concatenation

String concatenation can be indirectly used to convert Java primitive values to a string as given below.

Output

What is the best way to convert?

The toString method of the Short wrapper class is more efficient in terms of performance. String concatenation should be avoided mainly for the conversion purpose because,

a) It is difficult to visually understand that the purpose of the code is to convert.

b) String concatenation operation creates unnecessary temporary objects during the conversion process. String concatenation is achieved using the append method of the StringBuilder or StringBuffer class. So the code,

Will be executed like,

This example is a part of the Java Basic Examples and Java Type conversion Tutorial.

Please let me know your views in the comments section below.

About the author

Leave a Reply

Your email address will not be published.