Home » Java Float compareTo() method with Examples

Java Float compareTo() method with Examples

by Online Tutorials Library

Java Float int compareTo() Method

The compareTo() method of Java Float class compares two Float objects numerically. The sign of the integer value returned is same as that would be returned by the compare(float f1, float f2) method.

Syntax:

Parameters:

anotherFloat is the float parameter passed, which is to be compared.

Return Value:

This method returns the integer value of the argument.

  • Returns a zero if the invoked Float object is same as value of anotherFloat.
  • Return a positive value if the invoked Float object is greater than anotherFloat.
  • Return a negative value if the invoked Float object is less than anotherFloat.

Example 1

Test it Now

Output:

50.44 is greater.  Answer after subtraction is 40.44.  

Example 2

Test it Now

Output:

Enter your age  18  Congrats you are an adult now.From now you can vote.  

Example 3

Test it Now

Output:

Error:(17, 23) java: method compareTo in class java.lang.Float cannot be applied to given types;    required: java.lang.Float    found: no arguments    reason: actual and formal argument lists differ in length  

It is compulsory to pass an arguments in Float class compareTo() method, otherwise it will give you an error.

Next TopicJava Float

You may also like