DoubleBuffer equals() method in Java with Examples
The equals() method of java.nio.DoubleBuffer Class is used to check whether or not the given buffer is equal to another object.
Two double buffers are equal if, and only if,
- They have the same element type,
-
They have the same number of remaining elements, and
- The two sequences of remaining elements, considered independently of their starting positions, are pointwise
equal.
This method considers two double elements a and b to be equal if (a == b) || (Double.isNaN(a) && Double.isNaN(b)). The values -0.0 and +0.0 are considered to be equal, unlike Double.equals(Object).
A double buffer is not equal to any other type of object.
Syntax:
public boolean equals(Object ob)
Parameters: This method takes the ob, the object to which this buffer is to be compared, as a parameter.
Return Value: This method returns true if, and only if, this buffer is equal to the given object.
Below are the examples to illustrate the equals() method:
Example 1:
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int capacity1 = 10 ;
int capacity2 = 10 ;
try {
DoubleBuffer db1 = DoubleBuffer.allocate(capacity1);
DoubleBuffer db2 = DoubleBuffer.allocate(capacity2);
db1.put( 8 .56F);
db1.put( 2 , 9 .61F);
db1.rewind();
db2.put( 8 .56F);
db2.put( 2 , 9 .61F);
db2.rewind();
System.out.println( " DoubleBuffer 1: "
+ Arrays.toString(db1.array()));
System.out.println( " DoubleBuffer 2: "
+ Arrays.toString(db2.array()));
boolean dbb = db1.equals(db2);
if (dbb)
System.out.println( "both are equal" );
else
System.out.println( "both are not equal" );
}
catch (IllegalArgumentException e) {
System.out.println( "IllegalArgumentException catched" );
}
catch (ReadOnlyBufferException e) {
System.out.println( "ReadOnlyBufferException catched" );
}
}
}
|
Output:
DoubleBuffer 1: [8.5600004196167, 0.0, 9.609999656677246, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
DoubleBuffer 2: [8.5600004196167, 0.0, 9.609999656677246, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
both are equal
Example 1:
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int capacity1 = 10 ;
int capacity2 = 5 ;
try {
DoubleBuffer db1 = DoubleBuffer.allocate(capacity1);
DoubleBuffer db2 = DoubleBuffer.allocate(capacity2);
db1.put( 8 .56F);
db1.put( 2 , 9 .61F);
db1.rewind();
db2.put( 8 .56F);
db2.put( 2 , 9 .61F);
db2.rewind();
System.out.println( " DoubleBuffer 1: "
+ Arrays.toString(db1.array()));
System.out.println( " DoubleBuffer 2: "
+ Arrays.toString(db2.array()));
boolean dbb = db1.equals(db2);
if (dbb)
System.out.println( "both are equal" );
else
System.out.println( "both are not equal" );
}
catch (IllegalArgumentException e) {
System.out.println( "IllegalArgumentException catched" );
}
catch (ReadOnlyBufferException e) {
System.out.println( "ReadOnlyBufferException catched" );
}
}
}
|
Output:
DoubleBuffer 1: [8.5600004196167, 0.0, 9.609999656677246, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
DoubleBuffer 2: [8.5600004196167, 0.0, 9.609999656677246, 0.0, 0.0]
both are not equal
Last Updated :
04 Dec, 2018
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...