Wednesday, July 3, 2024
HomeLanguagesJavaHow to Convert a Short value to String value in Java with...

How to Convert a Short value to String value in Java with Examples

Given a Short value in Java, the task is to convert this short value to string type.

Examples:

Input: 1
Output: "1"

Input: 3
Output: "3"

Approach 1: (Using + operator)
One method is to create a string variable and then append the short value to the string variable. This will directly convert the short value to string and add it in the string variable.

Below is the implementation of the above approach:

Example 1:




// Java Program to convert short value to String value
  
class GFG {
  
    // Function to convert short value to String value
    public static String
    convertShortToString(short shortValue)
    {
  
        // Convert short value to String value
        // using + operator method
        String stringValue = "" + shortValue;
  
        return (stringValue);
    }
  
    // Driver code
    public static void main(String[] args)
    {
  
        // The short value
        short shortValue = 1;
  
        // The expected string value
        String stringValue;
  
        // Convert short to string
        stringValue
            = convertShortToString(shortValue);
  
        // Print the expected string value
        System.out.println(
            shortValue
            + " after converting into string = "
            + stringValue);
    }
}


Output:

1 after converting into string = 1

Approach 2: (Using String.valueOf() method)
The simplest way to do so is using valueOf() method of String class in java.lang package. This method takes the short value to be parsed and returns the value in String type from it.

Syntax:

String.valueOf(shortValue);

Below is the implementation of the above approach:

Example 1:




// Java Program to convert short value to String value
  
class GFG {
  
    // Function to convert short value to String value
    public static String
    convertShortToString(short shortValue)
    {
  
        // Convert short value to String value
        // using valueOf() method
        return String.valueOf(shortValue);
    }
  
    // Driver code
    public static void main(String[] args)
    {
  
        // The short value
        short shortValue = 1;
  
        // The expected string value
        String stringValue;
  
        // Convert short to string
        stringValue
            = convertShortToString(shortValue);
  
        // Print the expected string value
        System.out.println(
            shortValue
            + " after converting into string = "
            + stringValue);
    }
}


Output:

1 after converting into string = 1

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments