Java Basic Examples

Java convert byte to String example

This example shows how to convert byte to String in Java. This example also shows how to convert byte to String using several methods and the best way among them.

How to convert byte to String in Java?

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

1) Convert byte to String using Byte wrapper class

Use toString method of Byte wrapper class to convert from byte to String in Java.

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

Output

2) Convert byte to String using String concatenation

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

Output

What is the best way to convert byte to String?

toString method of 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 StringBuffer’s or StringBuilder’s append method. So the code,

Will run like,

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

Want to learn quickly?
Try one of the many quizzes. I promise you will not be disappointed.

Tags