Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator
numberutils

What is NumberUtils.toShort() in Java?

abhilash

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

toShort() is a staticThis describes the methods in Java that can be called without creating an object of the class. method of the NumberUtils class that is used to convert the given string to a short value.

One variant of the method accepts a default value, which is returned if the conversion fails.

How to import NumberUtils

The definition of NumberUtils can be found in the Apache Commons Lang package, which we can add to the Maven project by adding the following dependency to the pom.xml file:


<dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.12.0</version>
</dependency>

For other versions of the commons-lang package, refer to the Maven Repository.

You can import the NumberUtils class as follows:


import org.apache.commons.lang3.math.NumberUtils;

Syntax


public static short toShort(final String str, final short defaultValue)

Parameters

  • final String str: This is the string to convert.

  • final short defaultValue: This is the default value to return.

Return value

This method returns a short value. If the conversion fails, it returns the default value.

Overloaded methods

  • public static short toShort(final String str)

Code

import org.apache.commons.lang3.math.NumberUtils;
public class Main{
public static void main(String[] args){
// Example 1
String stringToConvert = "23";
short defaultValue = 24;
System.out.printf("The output of the method NumberUtils.toShort(%s, %s) is %s", stringToConvert, defaultValue, NumberUtils.toShort(stringToConvert, defaultValue));
System.out.println();
// Example 2
stringToConvert = "233sdf";
defaultValue = 24;
System.out.printf("The output of the method NumberUtils.toShort(%s, %s) is %s", stringToConvert, defaultValue, NumberUtils.toShort(stringToConvert, defaultValue));
System.out.println();
}
}

Example 1

  • string to convert = 23
  • default value = 24

The method returns 23, as the conversion is successful.

Example 2

  • string to convert = 233sdf
  • default value = 24

The method returns 24, as the conversion is unsuccessful.

Output

The output of the code is as follows:


The output of the method NumberUtils.toShort(23, 24) is 23
The output of the method NumberUtils.toShort(233sdf, 24) is 24

RELATED TAGS

java
communitycreator
numberutils

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring