Skip to content

Java convert byte to String example

This example shows how to convert byte to String in Java using the toString and + operator. It also shows what is the best way to convert byte to string.

How to convert the byte to String in Java?

There are several ways in which primitive byte type can be converted to a String in Java.

1) Using the Byte wrapper class

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

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

Output

2) Using String concatenation

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

Output

What is the best way to convert byte to String?

The toString method of the Byte wrapper class is more efficient in terms of the 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 statement is to conversion.

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 run 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.