Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Java Program to convert string to byte
Suppose you are given a String named "str". Now, your task is to write a Java program to convert the given string to Byte.
String is a class in Java which stores sequence of characters within double quotes and Byte is a wrapper class of java.lang package which wraps value of byte datatype.
Example Scenario:
Input: String str = "65"; Output: res = 65
Using Byte.valueOf() Method
The valueOf() method of Java Byte class is used to convert given string into its corresponding Byte object. It accepts a single numeric string as a parameter value and returns it as a Byte object.
Example
The following Java program shows how to convert a given String into Byte.
public class Demo {
public static void main(String[] args) {
String str = "65";
System.out.println("Given String is: " + str);
// checking type before converting
System.out.println("Type before converting: " + ((Object)str).getClass().getSimpleName());
// converting to byte using Byte.valueOf()
byte res = Byte.valueOf(str);
System.out.println("String after converting to Byte: " + res);
// checking type after converting
System.out.println("Type after converting: " + ((Object)res).getClass().getSimpleName());
}
}
On running, this code generates following result ?
Given String is: 65 Type before converting: String String after converting to Byte: 65 Type after converting: Byte
Using Byte.parseByte() Method
The parseByte() method of Java Byte class parses the specified string argument as a signed decimal byte.
Example
In this Java program, we are passing a signed numeric string and converting it into byte using parseByte() method.
public class Demo {
public static void main(String[] args) {
String str = "-127";
System.out.println("Given String is: " + str);
// checking type before converting
System.out.println("Type before converting: " + ((Object)str).getClass().getSimpleName());
// converting to byte using Byte.parseByte()
byte res = Byte.parseByte(str);
System.out.println("String after converting to Byte: " + res);
// checking type after converting
System.out.println("Type after converting: " + ((Object)res).getClass().getSimpleName());
}
}
Output of the above is obtained as ?
Given String is: -127 Type before converting: String String after converting to Byte: -127 Type after converting: Byte
Handling Errors while Converting String to Byte
Both Byte.parseByte() and Byte.valueOf() methods expects a string that represents a value within the range of byte data type, i.e. a number between -128 and 127. Therefore, you need to pass numeric string within its range otherwise you may encounter NumberFormatException.
Example
Now, let's see the practical demonstration ?
public class Demo {
public static void main(String[] args) {
// string value is greater than Byte range
String str = "130";
System.out.println("Given String is: " + str);
// converting to byte
byte res = Byte.valueOf(str);
System.out.println("String after converting toByte: " + res);
}
}
When you execute this code, it will give following error message ?
Given String is: 130 Exception in thread "main" java.lang.NumberFormatException: Value out of range. Value:"130" Radix:10 at java.base/java.lang.Byte.parseByte(Byte.java:195) at java.base/java.lang.Byte.valueOf(Byte.java:249) at java.base/java.lang.Byte.valueOf(Byte.java:275) at Demo.main(Demo.java:7)
