Q:

Java program to convert string to byte

belongs to collection: Java Conversion Programs

0

Java program to convert string to byte

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

In this program, we will read a numeric string from the user and convert the input string into a byte using 2 different methods.

Program/Source Code:

The source code to convert string to byte is given below. The given program is compiled and executed successfully.

// Java program to convert 
// string to byte

import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner X = new Scanner(System.in);
    String str;

    System.out.print("Enter string: ");
    str = X.next();

    byte byteValue = 0;

    //Using byteValue() method.
    byteValue = Byte.valueOf(str).byteValue();
    System.out.println("Byte value: " + byteValue);

    //Using parseByte() method.
    byteValue = Byte.parseByte(str);
    System.out.println("Byte value: " + byteValue);
  }
}

Output:

Enter string: 123
Byte value: 123
Byte value: 123

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java program to convert a string into a short inte... >>
<< Conversion from String to Float in Java...