Java Short.parseShort(String s) example

Java Short.parseShort(String s) example

By Yashwant Chavan, Views 9, Date 16-May-2018

In this java tutorial, You will learn how to use parseShort(String s) method of Short class. This method, Parses the string argument as a signed decimal short. The characters in the string must all be decimal digits, except that the first character may be an ASCII minus sign '-' ('\u002D') to indicate a negative value or an ASCII plus sign '+' ('\u002B') to indicate a positive value. The resulting short value is returned, exactly as if the argument and the radix 10 were given as arguments to the parseShort(java.lang.String, int) method.

tags Java

Parses the string argument as a signed decimal short.

Syntax

public static short parseShort(String s) throws NumberFormatException

Parameters

s - a String containing the short representation to be parsed.

Returns

The short value represented by the argument in decimal.

Throws

NumberFormatException - If the string does not contain a parsable short.

Java Short parseShort(String s) example

  • Assign two String values.
  • Short.parseShort(firstValue);It will parses the string firstValue as signed decimal short.
  • Short.parseShort(secondValue);It will parses the string secondValue as signed decimal short.
  • Print the values after parsing.
package com.technicalkeeda;

public class App {

 public static void main(String[] args) {

  String firstValue = "225";
  String secondValue = "-20";

  short result1 = Short.parseShort(firstValue);
  short result2 = Short.parseShort(secondValue);

  System.out.println("The parse value for firstValue :- " + result1);
  System.out.println("The parse value for secondValue :- " + result2);
 }
}

output

The short value for firstValue :- 225
The short value for secondValue :- -20

Suggested Posts

Yashwant

Yashwant

Hi there! I am founder of technicalkeeda.com and programming enthusiast. My skills includes Java,J2EE, Spring Framework, Nodejs, PHP and lot more. If you have any idea that you would want me to develop? Lets connect: yashwantchavan[at][gmail.com]