java.lang.Byte valueOf(String s)

Description

The valueOf(String s) method of Byte class 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.

In other words, this method returns a Byte object equal to the value of:

new Byte(Byte.parseByte(s))

Important Notes:

  • The method valueOf(String s) throws NumberFormatException, if the String does not contain a parsable byte.

Method Syntax

public static Byte valueOf(String s)
throws NumberFormatException

Method Argument

Data Type Parameter Description
String s the string to be parsed

Method Returns

The valueOf(String s) method of Byte class returns a Byte object holding the value represented by the string argument.

Compatibility

Requires Java 1.1 and up

Java Byte valueOf(String s) Example

Below is a simple java example on the usage of valueOf(String s) method of Byte class.

package com.javatutorialhq.java.examples;

/*
 * This example source code demonstrates the use of 
 * valueOf(byte b) method of Byte class.
 */

public class ByteValueOfExample {

	public static void main(String[] args) {
		
		// initialize rectangle dimension
		String length = "10";
		String width = "12";
		// convert string input into byte
		byte lengthConverted = Byte.valueOf(length);
		byte widthConverted = Byte.valueOf(width);
		
		// calculate are of rectangle
		byte area = (byte)(lengthConverted * widthConverted);
		
		// print the result
		System.out.print("Area of rectangle is "+area);
		
		

		

	}

}

Basically on the above example, we have declared a new primitive type byte and then eventually converted into Byte object using the valueOf() method. As you would have noticed already, the printed values are the same. But remember that the result is now a Byte object. Anyway because of autoxing and unboxing features which have been introduced at the newer version of java, the valueOf() method renders obsolete.

Sample Output

Below is the sample output when you run the above example.

java Byte valueOf(String s) example output