Java - Short Class toString() Method

Short class toString() method: Here, we are going to learn about the toString() method of Short class with its syntax and example. By Preeti Jain Last updated : March 18, 2024

Syntax

public String toString();
public static String toString(short value);

Short class toString() method

  • toString() method is available in java.lang package.
  • toString() method is used to represent String denoted by this Short object.
  • toString(short value) method is used to represent String denoted by the given argument of short type.
  • These methods don't throw an exception at the time of String representation.
  • toString() is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • toString(int value) is a static method, it is accessible with the class name too and, if we try to access this method with the class object then also we will not get an error.

Parameters

  • In the first case toString(), we don't pass any parameter or value.
  • In the second case toString(short value), we pass only one parameter of short type it represents the short value to be converted.

Return Value

In the first case, the return type of this method is String - it returns the String representation of this Short object.

In the second case, the return type of this method is String - it returns the String representation of the given argument is of short type.

Example

// Java program to demonstrate the example 
// of toString () method of Short class

public class ToStringOfShortClass {
    public static void main(String[] args) {
        short s1 = 100;
        short s2 = 200;

        // Object initialization
        Short ob1 = new Short(s1);
        Short ob2 = new Short(s2);

        // Display ob1,ob2 values
        System.out.println("ob1: " + ob1);
        System.out.println("ob2: " + ob2);

        // It represents the string of this Short object
        String value1 = ob1.toString();

        // It represents the string of the given short parameter
        String value2 = Short.toString(ob2);

        // Display result values
        System.out.println("ob1.toString(): " + value1);
        System.out.println("Short.toString(ob2): " + value2);
    }
}

Output

ob1: 100
ob2: 200
ob1.toString(): 100
Short.toString(ob2): 200

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.