Given a Byte value in Java, the task is to convert this byte value to string type.
Examples:
Input: 1
Output: "1"
Input: 3
Output: "3"
Approach 1: (Using + operator)
One method is to create a string variable and then append the byte value to the string variable with the help of + operator. This will directly convert the byte value to a string and add it in the string variable.
Below is the implementation of the above approach:
Example 1:
class GFG {
public static String
convertByteToString( byte byteValue)
{
String stringValue = "" + byteValue;
return (stringValue);
}
public static void main(String[] args)
{
byte byteValue = 1 ;
String stringValue;
stringValue
= convertByteToString(byteValue);
System.out.println(
byteValue
+ " after converting into string = "
+ stringValue);
}
}
|
Output:
1 after converting into string = 1
Approach 2: (Using String.valueOf() method)
The simplest way to do so is using valueOf() method of String class in java.lang package. This method takes the byte value to be parsed and returns the value in String type from it.
Syntax:
String.valueOf(byteValue);
Below is the implementation of the above approach:
Example 1:
class GFG {
public static String
convertByteToString( byte byteValue)
{
return String.valueOf(byteValue);
}
public static void main(String[] args)
{
byte byteValue = 1 ;
String stringValue;
stringValue
= convertByteToString(byteValue);
System.out.println(
byteValue
+ " after converting into string = "
+ stringValue);
}
}
|
Output:
1 after converting into string = 1
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
30 Jan, 2020
Like Article
Save Article