Java - Byte Class decode() Method

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

Short class decode() method

  • decode() method is available in java.lang package.
  • decode() method is used to decode the given String value into a Byte value.
  • decode() method is a static method, it is accessible with the class name too and if we try to access the method with the class object then also we will not get an error.
  • decode() method may throw a NumberFormatException at the time of decoding a String to a Byte.
    NumberFormatException: In this exception, if the given string parameter does not have a parsable byte.

Syntax

public static Byte decode(String str);

Parameters

  • String str – represents the String to decode.

Return Value

The return type of this method is Byte, it returns the Byte holding a byte value represented by the argument of String type.

Example

// Java program to demonstrate the example 
// of decode(String str) method of Byte class

public class DecodeOfByteClass {
    public static void main(String[] args) {
        // Variables initialization
        byte by = 100;
        String str = "20";

        // Byte object initialized with "byte" value
        Byte value1 = new Byte(by);

        // Display value1 result
        System.out.println("value1:" + value1);

        // It returns an Byte object holding the byte value 
        // denoted by the given String argument by calling 
        // value1.decode(str)

        Byte result = value1.decode(str);

        // Display result
        System.out.println("value1.decode(str): " + result);
    }
}

Output

value1:100
value1.decode(str): 20

Comments and Discussions!

Load comments ↻





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