Write Java program to Convert byte into the string


This Java program prompts the user to input a byte value and then converts the byte value to a string using the Byte.toString() method. The resulting string is then printed to the console.

Here's a brief explanation of the program:

  • The program starts by importing the java.util.Scanner class for input and creating a Scanner object.
  • The main() method declares a String variable str and a byte variable byte_val and initializes the latter to 0.
  • The program prompts the user to enter a byte value using the System.out.print() method and stores the user's input in byte_val using the Scanner.nextByte() method.
  • The Byte.toString() method is used to convert the byte value to a String and store it in the str variable.
  • Finally, the program prints the resulting string to the console using the System.out.println() method.

Note that the Byte.toString() method is a static method that takes a byte value as its argument and returns a String representation of that value.

Source Code

import java.util.Scanner;
public class ByteToString
{
	public static void main(String[] args)
	{
		Scanner input = new Scanner(System.in);
		String str;
		byte byte_val = 0;
 
		System.out.print("Enter the Byte Value : ");
		byte_val = input.nextByte();
 
		//Convert byte to string
		str = Byte.toString(byte_val);
		System.out.println("String value : " + str);
	}
}

Output

Enter the Byte Value : 23
String value : 23