1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
/* Convert Java String to Byte example This example shows how we can convert String object to Byte object. */ public class StringToByteExample { public static void main(String[] args) { //We can convert String to Byte using following ways. //1. Construct Byte using constructor. Byte bObj1 = new Byte("100"); System.out.println(bObj1); //2. Use valueOf method of Byte class. This method is static. String str = "100"; Byte bObj2 = Byte.valueOf(str); System.out.println(bObj2); //Please note that both method can throw a NumberFormatException if //string can not be parsed. } } /* Output of the program would be : 100 100 */ |
public class StringToByteExample {
public static void main(String[] args) {
//We can convert String to Byte using following ways.
//1. Construct Byte using constructor.
Byte bObj1 = new Byte(“100”);
System.out.println(bObj1);
//2. Use valueOf method of Byte class. This method is static.
String str = “100”;
Byte bObj2 = Byte.valueOf(str);
System.out.println(bObj2);
//Please note that both method can throw a NumberFormatException if
//string can not be parsed.
}
}
/*
Output of the program would be :
100
100
*/