What is the FloatBuffer compareTo() method in Java?

The compareTo() method of the class java.nio.FloatBuffer is used to compare two buffers. Two buffers are compared by comparing their sequence of remaining elements lexicographically, without regard to the starting point of the buffers.

Note:

  • Two floats are compared the same way as the Float.compare() method. The only difference is that unlike Float.compare(), -0.0 and +0.0 are considered equal by the FloatBuffer.compareTo() method.
  • Float.NaN is considered to be greater than all other float values by the FloatBuffer.compareTo() method.

Declaration

The FloatBuffer.compareTo() method can be declared as:

buff1.compareTo(buff2);
  • buff1: The first buffer, to be compared to buff2.
  • buff2: The second buffer, to be compared to buff1.

Return value

The FloatBuffer.compareTo() method returns an integer such that:

  • The return value is a positive integer if buff1 is greater than buff2.
  • The return value is 0 if buff1 is equal to buff2.
  • The return value is a negative integer if buff1 is less than buff2.

Examples

Example1

Consider the code snippet below, which demonstrates the use of the FloatBuffer.compareTo() method:

import java.nio.*;
import java.util.*;
public class main {
public static void main(String[] args) {
int n1 = 5;
int n2 = 4;
try {
FloatBuffer buff1 = FloatBuffer.allocate(n1);
buff1.put(1.5F);
buff1.put(4.6F);
System.out.println("buff1: " + Arrays.toString(buff1.array()));
FloatBuffer buff2 = FloatBuffer.allocate(n2);
buff2.put(3.6F);
buff2.put(4.9F);
System.out.println("buff2: " + Arrays.toString(buff2.array()));
int foo = buff1.compareTo(buff2);
System.out.println("\nbuff1 compareTo to buff2: " + foo);
} catch (IllegalArgumentException e) {
System.out.println("Error!!! IllegalArgumentException");
} catch (ReadOnlyBufferException e) {
System.out.println("Error!!! ReadOnlyBufferException");
}
}
}

Explanation

  • A FloatBuffer buff1 is declared in line 8. Two floats are written to buff1 in lines 9-10.
  • A FloatBuffer buff2 is declared in line 13. Two floats are written to buff2 in lines 14-15.
  • The FloatBuffer.compareTo() method is used in line 18 to see if buff1 and buff2 are equal. The FloatBuffer.compareTo() method returns a positive integer, which means that buff1 is greater than buff2.

Example 2

Consider the code snippet below, which compares two equal buffers:

import java.nio.*;
import java.util.*;
public class main {
public static void main(String[] args) {
int n1 = 4;
int n2 = 4;
try {
FloatBuffer buff1 = FloatBuffer.allocate(n1);
buff1.put(1.5F);
buff1.put(4.6F);
System.out.println("buff1: " + Arrays.toString(buff1.array()));
FloatBuffer buff2 = FloatBuffer.allocate(n2);
buff2.put(3.6F);
buff2.put(4.9F);
System.out.println("buff2: " + Arrays.toString(buff2.array()));
int foo = buff1.compareTo(buff2);
System.out.println("\nbuff1 compareTo to buff2: " + foo);
} catch (IllegalArgumentException e) {
System.out.println("Error!!! IllegalArgumentException");
} catch (ReadOnlyBufferException e) {
System.out.println("Error!!! ReadOnlyBufferException");
}
}
}

Explanation

  • A FloatBuffer buff1 is declared in line 8. Two floats are written to buff1 in lines 9-10.
  • A FloatBuffer buff2 is declared in line 13. Two floats are written to buff2 in lines 14-15.
  • The FloatBuffer.compareTo() method is used in line 18 to see if buff1 and buff2 are equal. The FloatBuffer.compareTo() method returns a positive integer, which means that buff1 is equal to buff2.
New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved