java.lang.Float compareTo(Float anotherFloat)

Description

The compareTo() method of Float class is used to compare this Float object to the float method argument. This is a little bit different to the compare method because in compare method we put two floats together as method argument, while the compareTo method uses the current Float value to compare to the float method argument. However the compareTo method returns the same value as that of the compare method.

Important Notes:

  • specified by compareTo in interface Comparable<Float>

Method Syntax

public int compareTo(Float anotherFloat)

Method Argument

Data Type Parameter Description
float anotherFloat the Float to be compared.

Method Returns

The compareTo() method of Float class returns the value 0 if anotherFloat is numerically equal to this Float; a value less than 0 if this Float is numerically less than anotherFloat; and a value greater than 0 if this Float is numerically greater than anotherFloat.

Compatibility

Java 1.2

Discussion

The compareTo method of Float class is specified by Comparable interface. This method is simply another way to compare this two float value on this case the Float object to the float method argument. The following values will be returned by this method:

  • 0 if the method argument float is equal to this Float object.
  • a value greater than 0 if this Float is numerically greater than the float method argument.
  • a value less than 0 if this Float object is numerically less than the float method argument.

Java Float compareTo() Example

Below is a simple java example on the usage of compareTo() method of Float class.

package com.javatutorialhq.java.examples;

import java.util.Scanner;

import static java.lang.System.*;

/*
 * This example source code demonstrates the use of 
 * compareTo(float anotherFloat) method of Float class.
 */

public class FloatCompareToExample {

	public static void main(String[] args) {

		Float value = new Float(3.12f);

		// Ask user input for any number
		System.out.print("Enter a Number:");
		// declare the scanner object
		Scanner scan = new Scanner(System.in);
		// use scanner to get value from user console
		Float anotherFloat = scan.nextFloat();
		// close the scanner object
		scan.close();
		int result = value.compareTo(anotherFloat);
		System.out.println("Result:" + result);

		if (result == 1) {
			out.println("This number is greater than the value entered");
		} else if (result == 0) {
			out.println("This number is equal to the value entered");
		} else {
			out.println("This number is less than the value entered");
		}

	}

}

Basically on the above example, we just ask for user input on the console and then we use the scanner object to get the float input. We then compare the user input to the Float that has been declared at the beginning using the compareTo method. The result of the comparison will be used to get a meaningful printout.

Sample Output

Below is the sample output when you run the above example.

Float compareTo example output