Join
Blogs
Questions
Videos
Tags
Members
Search
 
 
Create your own blog, earn points and get popular!

Convert String to Byte example

  1. /*
  2.  * Convert String to Byte example. Program to convert Java
  3.  * String object to Byte object.
  4.  */
  5.  
  6. public class StringToByte {
  7.  
  8.  public static void main(String args[]){
  9.  
  10.   //new String object
  11.   String str = new String("2");
  12.  
  13.   //Use Byte(String) constructor
  14.   Byte bObj1 = new Byte(str);
  15.  
  16.   //OR Use static valueOf(String) method of Byte class
  17.   Byte bObj2 = Byte.valueOf(str); //throws NumberFormatException
  18.    
  19.   System.out.println("String converted to Byte: " + bObj1);
  20.  
  21.  }
  22.  
  23. }
No votes yet
Share this

Post new comment

2 + 2 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.