What is the DoubleBuffer compareTo() method in Java?
The compareTo() method of the class java.nio.DoubleBuffer 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 doubles are compared the same way as the
Double.compare()method. The only difference is that unlikeDouble.compare(), -0.0 and +0.0 are considered equal by theDoubleBuffer.compareTo()method.Double.NaNis considered to be greater than all other double values by theDoubleBuffer.compareTo()method.
Declaration
The DoubleBuffer.compareTo() method can be declared as:
buff1.compareTo(buff2);
buff1: The first buffer, to be compared tobuff2.buff2: The second buffer, to be compared tobuff1.
Return value
The DoubleBuffer.compareTo() method returns an integer such that:
- The return value is a positive integer if
buff1is greater thanbuff2. - The return value is 0 if
buff1is equal tobuff2. - The return value is a negative integer if
buff1is less thanbuff2.
Examples
Example1
Consider the code snippet below, which demonstrates the use of the DoubleBuffer.compareTo() method:
import java.nio.*;import java.util.*;public class main {public static void main(String[] args) {int n1 = 5;int n2 = 4;try {DoubleBuffer buff1 = DoubleBuffer.allocate(n1);buff1.put(1.5);buff1.put(4.6);System.out.println("buff1: " + Arrays.toString(buff1.array()));DoubleBuffer buff2 = DoubleBuffer.allocate(n2);buff2.put(3.6);buff2.put(4.9);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
DoubleBufferbuff1is declared in line 8. Two doubles are written tobuff1in lines 9-10. - A
DoubleBufferbuff2is declared in line 13. Two doubles are written tobuff2in lines 14-15. - The
DoubleBuffer.compareTo()method is used in line 18 to see ifbuff1andbuff2are equal. TheDoubleBuffer.compareTo()method returns apositive integer, which means thatbuff1is greater thanbuff2.
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 {DoubleBuffer buff1 = DoubleBuffer.allocate(n1);buff1.put(1.5);buff1.put(4.6);System.out.println("buff1: " + Arrays.toString(buff1.array()));DoubleBuffer buff2 = DoubleBuffer.allocate(n2);buff2.put(3.6);buff2.put(4.9);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
DoubleBufferbuff1is declared in line 8. Two doubles are written tobuff1in lines 9-10. - A
DoubleBufferbuff2is declared in line 13. Two doubles are written tobuff2in lines 14-15. - The
DoubleBuffer.compareTo()method is used in line 18 to see ifbuff1andbuff2are equal. TheDoubleBuffer.compareTo()method returns apositive integer, which means thatbuff1is equal tobuff2.
Free Resources
Copyright ©2026 Educative, Inc. All rights reserved