How to convert String to byte and byte to string in java


In this core java tutorial we will learn how to convert String to byte primitive type in java and convert String to byte object with program and examples in java. We will  also understand  When to use convert String to byte primitive type and when when to convert String to byte object in java.




Programs  to convert String to byte in java -
  • Program/Example 1 to How to convert String to byte primitive type in java.
  • Program/Example 2 to How to convert String to byte object in java.


Program/Example 1 to How to convert String to byte primitive type in java

Byte.parseByte(string) method returns primitive type byte object.

package convertStringToBytePkg;
public class ConvertStringToByteExample1 {
   public static void main(String[] args) {
          String string = "12";
          try {
                 byte b = Byte.parseByte(string);
                 System.out.println("byte number = " + b);
          } catch (NumberFormatException e) {
                 e.printStackTrace();
          }
   }
}
/*OUTPUT
byte number = 12
*/

If in case invalid string is passed which can't get converted to byte will throw NumberFormatException in java.




Program/Example 2 to How to convert String to byte object in java

Byte.valueOf(string) method returns byte object.

package convertStringToBytePkg;
public class ConvertStringToByteExample2 {
   public static void main(String[] args) {
          String string = "12";
          Byte byteObject  = Byte.valueOf(string);
          System.out.println("byte byteObject = " + byteObject);
         
   }
}
/*OUTPUT
byte byteObject = 12
*/





Programs to convert byte to String in java -
  • Program/Example 3 to How to convert byte primitive type to String  in java.
  • Program/Example 4 to How to convert byte object to String in java


Now, in this core java tutorial we will learn how to convert byte primitive type to String in java and convert byte object to String with program and examples in java. We will  also understand  When to use convert String to byte primitive type and when when to convert String to byte object in java.

Program/Example 3 to How to convert byte primitive type to String  in java.

String.valueOf(byte) method converts byte primitive type to String  in java.

package convertByteToStringPkg;
public class ConvertByteToStringExample1 {
   public static void main(String[] args) {
         
          byte b = 12;
          String string = String.valueOf(b);
          System.out.println("string = "+string);
  
   }
}
/*OUTPUT
string = 12

*/



Program/Example 4 to How to convert byte object to String in java
toString() method of java.lang.Byte class convert byte object to String in java.
package convertByteToStringPkg;
public class ConvertByteObjectToStringExample2 {
   public static void main(String[] args) {
          Byte byteObject = 12;
          String string = byteObject.toString();
          System.out.println("string = "+string);
  
   }
}
/*OUTPUT
string = 12
*/






Summary -




Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.



RELATED LINKS>

Merge two sorted arrays

Collection - List, Set and Map all properties in tabular form



eEdit
Must read for you :