Java - Short toString() Method



The Java Short toString() method retrieves the string representation of the given short value. The value is converted as signed decimal and is returned as a string.

A Short object in Java is an instance of the "Short" wrapper class, which stores the short primitive data type. However, there are instances when we need to convert a short to a String since this allows us to store larger numbers that cannot be stored in short or any other data types that hold the numbers, such as integers or float.

This method has two polymorphic variants; without arguments and with a short argument (discussed in the below syntax).

Syntax

Following is the syntax for Java Short toString() method −

public String toString() // without argument
or,
public static String toString(short s) // with a short argument

Parameters

  • s − This is the short to be converted. // second syntax

Return Value

This method returns a String representation of this object or the argument in base 10. (if passed).

Example

The code given below shows the usage of Java Short toString() method by creating a Short object. Thereafter returning a string object representing this short value without passing any argument −

import java.lang.*;
public class ShortDemo {
   public static void main(String[] args) {
      Short s = new Short("53");
      
      // returns a string representation
      String retval = s.toString();
      System.out.println("Value = " + retval);
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Value = 53

Example

The following example converts the short values to its equivalent string value by assigning the short value to the string object 'str' using try-catch statement −

import java.util.Scanner;
public class ShortDemo {
   public static void main(String[] args) {
      try {
         Short value1 = 98;
         
         // conversion to string
         String str = value1.toString();
         System.out.println("The equivalent string value is = " + str);
      } catch (Exception e) {
         System.out.println("Error: Invalid input!");
      }
      try {
         Short value2 = 0 * 64576;
         
         // conversion to string
         String str2 = value2.toString();
         System.out.println("The equivalent string value is = " + str2);
      } catch (Exception e) {
         System.out.println("Error: Invalid input!");
      }
   }
}

Output

Following is an output of the above code −

The equivalent string value is = 98
The equivalent string value is = 0

Example

Following is another example of this method −

import java.lang.*;
public class ShortDemo {
   public static void main(String[] args) {
      
      // create short object and assign value to it
      short sval = 50;   
      Short shortValue = new Short(sval);
      
      // returns a String object representing this Short value.
      System.out.println("Value is = " + shortValue.toString());
      
      // returns a String object representing the specified short
      System.out.println("Value of specified short is = " + Short.toString((short)sval));
   }
}   

Output

Output of the above code is as follows −

Value is = 50
Value of specified short is = 50

Example

In the following example we can also calling the toString() method by on a Short object, which has a negative value.

import java.lang.*; 
public class ShortDemo {
   public static void main(String[] args) {
      
      // create short object and assign value to it
      short sval = -50;
      Short shortValue = new Short(sval);
      
      // returns a String object representing this Short value.
      System.out.println("Value is = " + shortValue.toString());
   }
}  

Output

While executing the above code we get the following output −

Value is = -50
java_lang_short.htm
Advertisements