Java - Double Class toString() Method

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

Syntax

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

Double class toString() method

  • toString() method is available in Double class of java.lang package.
  • toString() method is used to get the string value of Double value/object (it is called with "this" object).
  • toString(double value) method is also used to get the string value of given double value (here, we pass the double value/object as an argument).
  • Both types of methods do not throw the exception at the time of conversion from Double to String.
  • Both types of methods are non-static methods, and accessible with the class object only and if we try to access the method with the class name then we will get an error.

Parameters

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

Return Value

In the first case, String – it returns the String representation of this Double object.

In the second case, String – it returns the String representation of the given argument is of double type.

Example

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

public class ToStringOfDoubleClass {
    public static void main(String[] args) {
        // Object initialization
        Double ob1 = new Double("10.20");
        Double ob2 = new Double("20.20");

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

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

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

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

Output

ob1:10.2
ob2:20.2
ob1.toString(): 10.2
Double.toString(ob2): 20.2

Comments and Discussions!

Load comments ↻






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