Java - Byte Class valueOf() Method

Short class valueOf() method: Here, we are going to learn about the valueOf() method of Short class with its syntax and example. By Preeti Jain Last updated : March 18, 2024

Syntax

public static Byte valueOf (byte value);
public static Byte valueOf (String value);
public static Byte valueOf (String value, int radix's);

Short class valueOf() method

  • valueOf() method is available in java.lang package.
  • valueOf(byte value) method is used to represent Byte object denoted by the given argument (value) is of byte type.
  • valueOf(String value) method is used to represent the Byte object holding the byte value denoted by the given argument (value) is of String type.
  • valueOf(String value, int radix's) method is used to represent a Byte object holding the byte value of the given argument (value) in the radix's given by the second argument.
  • valueOf(byte value) method does not throw an exception at the time of returning a Byte instance.
  • Similarly, valueOf(String value), valueOf(String value, int radix's) methods may throw a NumberFormatException at the time of returning an instance.
    NumberFormatException: In this exception, if String does not contain the parsable number.
  • These methods are static methods, they are accessible with the class name too and, if we try to access these methods with the class object then also we will not get an error.

Parameters

  • In the first case, byte value – represents the value of byte type.
  • In the second case, String value – represents the value of String type.
  • In the third case, String value, int radix's – In this method first parameter value represents the value of String type in the radix's given by the second parameter.

Return Value

In the first case, the return type of this method is Byte - it returns the Byte representation of this byte argument.

In the second case, the return type of this method is Byte - it returns the Byte representation of this String argument.

In the third case, the return type of this method is Byte - it returns the Byte representation of the String argument in the radix's given by the second argument.

Example

// Java program to demonstrate the example 
// of valueOf() method of Byte class

public class ValueOfByteClass {
    public static void main(String[] args) {
        byte b1 = 10;
        byte b2 = 20;
        byte b3 = 30;

        // Object initialization
        Byte ob1 = new Byte(b1);
        Byte ob2 = new Byte(b2);
        Byte ob3 = new Byte(b3);

        // Display ob1,ob2,ob3 values
        System.out.println("ob1: " + ob1);
        System.out.println("ob2: " + ob2);
        System.out.println("ob3: " + ob3);

        // It returns Byte object holding the value 
        // denoted by the given byte argument
        Byte value1 = ob1.valueOf(b2);

        // String object initialization for valueOf(String s)
        String s = "80";

        // It returns Byte object holding the value 
        // denoted by the given String argument
        Byte value2 = ob2.valueOf(s);

        // It returns Byte object holding the value 
        // denoted by the given String argument with radix 20
        Byte value3 = ob3.valueOf(s, b1);

        // Display result values
        System.out.println("ob1.valueOf(b2): " + value1);
        System.out.println("ob2.valueOf(s): " + value2);
        System.out.println("ob3.valueOf(s,b1): " + value3);
    }
}

Output

ob1: 10
ob2: 20
ob3: 30
ob1.valueOf(b2): 20
ob2.valueOf(s): 80
ob3.valueOf(s,b1): 80

Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.