Java.lang.Byte.valueOf() Method

Advertisements


Description

The java.lang.Byte.valueOf(String s) returns a Byte object holding the value given by the specified String. The argument is interpreted as representing a signed decimal byte, exactly as if the argument were given to the parseByte(java.lang.String) method.

The result is a Byte object that represents the byte value specified by the string.

Declaration

Following is the declaration for java.lang.Byte.valueOf() method

public static Byte valueOf(String s)throws NumberFormatException

Parameters

  • s - the string to be parsed

Return Value

This method returns a Byte object holding the value represented by the string argument.

Exception

  • NumberFormatException - If the String does not contain a parsable byte.

Example

The following example shows the usage of lang.Byte.valueOf() method.

package com.tutorialspoint;

import java.lang.*;

public class ByteDemo {

   public static void main(String[] args) {

      // create a String s and assign value to it
      String s = "+120";

      // create a Byte object b
      Byte b;

      /**
       *  static method is called using class name.
       *  assign Byte instance value of s to b
       */
      b = Byte.valueOf(s);

      String str = "Byte value of string " + s + " is " + b;

      // print b value
      System.out.println( str );
   }
}

Let us compile and run the above program, this will produce the following result:

Byte value of string +120 is 120



Advertisements
Advertisements