Java Reference
In-Depth Information
Integer.compare(int3, int1));
Integer.compare(int2, int1));
Just like in your math lessons at school, these comparison operators will determine
whether the first integer is equal to, greater than, or less than the second integer.
Straightforward and easy to use, these comparison operators are most often seen within
the context of an if statement.
4-4. Comparing Floating-Point Numbers
Problem
You need to compare two or more floating-point values in an application.
Solution #1
Use the Float object's compareTo() method to perform a comparison of one float
against another. The following example shows the compareTo() method in action:
Float float1 = new Float("9.675");
Float float2 = new Float("7.3826");
Float float3 = new Float("23467.373");
System.out.println(float1.compareTo(float3)); // Result:
-1
System.out.println(float2.compareTo(float3)); // Result:
-1
System.out.println(float1.compareTo(float1)); // Result:
0
System.out.println(float3.compareTo(float2)); // Result:
1
The result of calling the compareTo() method is an integer value. A negative
result indicates that the first float is less than the float that it is being compared against.
A zero indicates that the two float values are equal. Lastly, a positive result indicates
that the first float is greater than the float that it is being compared against.
Search WWH ::




Custom Search