FloatBuffer equals() method in Java with examples

The java.nio.FloatBuffer class has equals() function. To determine whether the buffer that is provided is equal to another object, utilize the FloatBuffer Class.

If and only if two float buffers are equivalent, then

  • The element type is the same for both.
  • The number of remaining elements is the same for each.
  • Taking into consideration their initial positions separately, the two remaining element sequences are point-wise equivalent.

When (a == b) || (Float.isNaN(a) && Float.isNaN(b)), has two float elements, a and b, are considered equivalent by this method. In contrast to Float.equals(Object), the values -0.0 and +0.0 are regarded as equal. There is no other type of object that is equivalent to a float buffer.

Syntax:

Parameters: The object to which this buffer is to be compared is called the obj, and it is passed as an argument to this method.

Return Value: The method returns true if and only if the given object is equal to this buffer.

Example 1:

The Java code shows how to create and compare two instances of FloatBuffer, each with a capacity of twelve. Specific float values are placed at predetermined places in both buffers, which have the same size allocation. The values are added, and then the buffers are returned to their initial places. After printing the contents of both buffers, the equals method is used to compare them for equality. A message saying "Both are Equal" is printed if the contents are the same; if not, it says "Both are not Equal."

Implementation:

FileName: FloatBufferEqualsExample1.java

Output:

 
The FloatBuffer 1 is: [4.65, 0.0, 0.0, 7.84, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
The FloatBuffer 2 is: [4.65, 0.0, 0.0, 7.84, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
Both are Equal   

Example 2:

Two FloatBuffer objects with differing capacity (12 and 10) are created and compared by the Java code. The sizes of the two buffers are assigned, and certain float values are added at particular points. The buffers are rewound to reset their locations to zero after the values have been added. Both buffers' contents are printed, and the equals method is used to compare them for equality. The software prints "Both are not Equal" since the buffers are not considered as equal because of their different capacities.

Implementation:

FileName: FloatBufferEqualsExample2.java

Output:

 
The FloatBuffer 1 is: [4.65, 0.0, 0.0, 7.84, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
The FloatBuffer 2 is: [4.65, 0.0, 0.0, 7.84, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
Both are not Equal