Byte class defines static methods to convert string value to byte value.

ReturnMethodSummary
static BytevalueOf(byte b)Returns a Byte instance representing the specified byte value.
static BytevalueOf(String s)Returns a Byte object holding the value given by the specified String.
static BytevalueOf(String s, int radix)Returns a Byte object holding the value extracted from the specified String when parsed with the radix given by the second argument.
static byteparseByte(String s)Parses the string argument as a signed decimal byte.

public class Main {
  public static void main(String[] args) {
    System.out.println("parse string to byte:"+Byte.parseByte("10"));
  }
}

The output:


parse string to byte:10

ReturnMethodSummary
static byteparseByte(String s, int radix)Parses the string argument as a signed byte in the radix specified by the second argument.

In the following code we convert string 10 based on radix 8.


public class Main {
  public static void main(String[] args) {

    System.out.println("parse string to byte:"+Byte.parseByte("10", 8));
  }
}

The output:


parse string to byte:8

And we use the valueOf methods in the same way:


public class Main {
  public static void main(String[] args) {
    
    System.out.println("parse string to byte:"+Byte.valueOf("10"));
    System.out.println("parse string to byte:"+Byte.valueOf("10", 8));
  }
}

The output:


parse string to byte:10
parse string to byte:8
java2s.com  |  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.