Java - Long Class toString() Method

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

Syntax

    public String toString();
    public static String toString(long value);
    public static String toString(long value, int radix's);

Long class toString() method

  • toString() method is available in java.lang package.
  • toString() method is used to represent String denoted by this Long object.
  • toString(long value) method is used to represent String denoted by the given argument of long type.
  • toString(long value, int radix's) method is used to represent String of the given argument of long type in the radix's given by the second argument.
  • These methods don't throw an exception at the time of String representation.
  • toString() method 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(long value) and toString(long value, int radix's) are the static methods, they are accessible with the class name too and, if we try to access these methods 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(long value), we pass only one parameter of the long type it represents the long value to be converted.
  • In the third case toString(long value, int radix's), we pass two parameters of long type it represents the long value to be converted and the parameter radix's represents the radix to be used in String denotation.

Return Value

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

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

In the third case, the return type of this method is String - it returns the String representation of the given argument is of long type in the given radix.

Example

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

public class ToStringOfLongClass {
    public static void main(String[] args) {
        // Object initialization
        Long ob1 = new Long(100);
        Long ob2 = new Long(200);

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

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

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

        // It represents the string of the given long parameter with radix 20
        String value3 = Long.toString(ob2, 20);

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

Output

ob1: 100
ob2: 200
ob1.toString(): 100
Long.toString(ob2): 200
Long.toString(ob2,20): a0

Comments and Discussions!

Load comments ↻






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