ByteBuffer equals() in Java

equals(): This method is available in java.nio.ByteBuffer class of Java.

Syntax:

boolean java.nio.ByteBuffer.equals(Object ob)

This method tells whether or not this buffer is equal to another object.

Two-byte buffers are equal if, and only if:

1. They have the same element type.

 2. They have the same number of remaining elements.

3. The two sequences of remaining elements considered independently of their starting positions, are point-wise equal.

Note: A byte buffer is not equal to any other type of object.

Parameters: ob The object to which this buffer is to be compared.

Returns: true if, and only if, this buffer is equal to the given object.

Exceptions: NA

Approach

Java

import java.nio.ByteBuffer;

public class ByteBufferequals {
    public static void main(String[] args) {

        byte array[] = { 1234 };
        ByteBuffer bb = ByteBuffer.wrap(array);

        // create object of type ByteBuffer
        Object obj = ByteBuffer.wrap(array);
        System.out.println(bb.equals(obj));

    }
}

Output:

true

No comments:

Post a Comment