Java - Float Class toString() Method

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

Syntax

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

Float class toString() method

  • toString() method is available in java.lang package.
  • toString() method is used to represent String denoted by this Float object.
  • toString(float value) method is used to represent String denoted by the given argument of float type.
  • These methods do not throw an exception at the time of conversion from Float to String.
  • They are non-static methods, they are 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(float value), we pass only one parameter of the float type it represents the float 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 Float object.

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

Example

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

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

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

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

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

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

Output

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

Comments and Discussions!

Load comments ↻





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