Wednesday, November 20, 2024
Google search engine
HomeLanguagesJavaJava.lang.Short toString() method in Java with Examples

Java.lang.Short toString() method in Java with Examples

toString(short)

The static toString() method of java.lang.Short returns a new String object representing the specified short. The radix is assumed to be 10.This is a static method hence no object of Short class is required for calling this method.

Syntax:

public static String toString(short b)

Parameters: This method accepts a parameter b which is the short value for which string representation is required.

Below is the implementation of toString() method in Java:

Example 1:




// Java program to demonstrate working
// of java.lang.Short.toString() method
  
import java.lang.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        short a = 515;
  
        // using toString()
        System.out.println("String value: "
                           + Short.toString(a));
    }
}


Output:

String value: 515

Example 2:




// Java program to demonstrate working
// of java.lang.Short.toString() method
  
import java.lang.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        short a = 51;
  
        // using toString()
        System.out.println("String value: "
                           + Short.toString(a));
    }
}


Output:

String value: 51

toString()

The non-static toString() method of java.lang.Short returns a String object representing this Short object value. The value is converted to signed decimal representation and returned as a string, exactly as if the short value were given as an argument to the toString(short) method.

Syntax:

public String toString()

Parameters: This method do not accepts any parameter.

Returns: This method returns a string representation of the value of this Short object in base 10.

Below is the implementation of toString() method in Java:

Example 1:




// Java program to demonstrate working
// of java.lang.Short.toString() method
  
import java.lang.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
        short a1 = 515;
  
        // create Short object
        Short a = new Short(a1);
  
        // using toString()
        System.out.println("String value: "
                           + a.toString());
    }
}


Output:

String value: 515

Example 2:




// Java program to demonstrate working
// of java.lang.Short.toString() method
  
import java.lang.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
        short a1 = 51;
  
        // create Short object
        Short a = new Short(a1);
  
        // using toString()
        System.out.println("String value: "
                           + a.toString());
    }
}


Output:

String value: 51

Reference:

RELATED ARTICLES

Most Popular

Recent Comments