Convert byte to String in Java
Tags: String byte Convert DecimalFormat
In this Java core tutorial, we learn how to convert byte value into String value in Java via different solutions.
Using String.valueOf() method to instantiate String from a byte value
ConvertByteToStringExample1.java
public class ConvertByteToStringExample1 {
public static void main(String[] args) {
byte value1 = 12;
String value2 = String.valueOf(value1);
System.out.println("byte value: " + value1);
System.out.println("String value: " + value2);
}
}
byte value: 12
String value: 12
Using Byte.toString() method to convert a byte value into String
ConvertByteToStringExample2.java
public class ConvertByteToStringExample2 {
public static void main(String[] args) {
byte value1 = 29;
String value2 = Byte.toString(value1);
System.out.println("byte value: " + value1);
System.out.println("String value: " + value2);
}
}
byte value: 29
String value: 29
Using Byte.toString() method to return String value from a Byte object
ConvertByteToStringExample3.java
public class ConvertByteToStringExample3 {
public static void main(String[] args) {
Byte value1 = 14;
String value2 = value1.toString();
System.out.println("byte value: " + value1);
System.out.println("String value: " + value2);
}
}
byte value: 14
String value: 14
Using String.format() method to format a String from a byte value
ConvertByteToStringExample4.java
public class ConvertByteToStringExample4 {
public static void main(String[] args) {
byte value1 = 67;
String value2 = String.format("%d", value1);
System.out.println("byte value: " + value1);
System.out.println("String value: " + value2);
}
}
byte value: 67
String value: 67
Using DecimalFormat class to format a byte value into String
ConvertByteToStringExample5.java
import java.text.DecimalFormat;
public class ConvertByteToStringExample5 {
public static void main(String[] args) {
byte value1 = 123;
DecimalFormat decimalFormat = new DecimalFormat("#");
String value2 = decimalFormat.format(value1);
System.out.println("byte value: " + value1);
System.out.println("String value: " + value2);
}
}
byte value: 123
String value: 123
Happy Coding 😊