Home » Java programming language

Java Short class parseShort() method with example

Short class parseShort() method: Here, we are going to learn about the parseShort() method of Short class with its syntax and example.
Submitted by Preeti Jain, on October 16, 2019

Syntax:

    public static short parseShort(String str);
    public static short parseShort(String str, int radix's);

Short class parseShort() method

  • parseShort() method is available in java.lang package.
  • parseShort(String str) method is used to return the short value corresponding to the given String denotation or in other words we can say this method is used to convert string value to a short value.
  • parseShort(String str, int radix's) method is used to return the short value corresponding to the given String denotation as a signed short in the radix's given by the second argument.
  • parseShort(String str), parseShort(String str, int radix's) method may throw a NumberFormatException at the time of conversion from String to short.
    NumberFormatException: In this exception, if String does not contain the parsable number.
  • These methods are the 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.

Parameter(s):

  • In the first case, String value – represents the value of String type.
  • In the second case, String value, int radix's – 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 short - it returns the Short representation of this String argument.

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

Example:

// Java program to demonstrate the example 
// of parseShort() method of Short class

public class ParseShortOfShortClass {
    public static void main(String[] args) {
        // Variables initialization
        String str1 = "100";
        String str2 = "67";
        int radix = 20;

        // Object initialization
        Short s1 = new Short(str2);

        // It convert string into short by calling parseShort(str1) method
        // and store the result in another variable of short type
        short result = s1.parseShort(str1);

        // Display result
        System.out.println("s1.parseShort(str1): " + result);

        // It convert string into short with radix 20 by 
        // calling parseShort(str1,radix's) method
        // and store the result in a variable of short type
        result = s1.parseShort(str1, radix);

        // Display result
        System.out.println("s1.parseShort(str1,radix): " + result);
    }
}

Output

s1.parseShort(str1): 100
s1.parseShort(str1,radix): 400





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.