The equals()
method of CharBuffer
is quite tricky.
From here I got that it makes char-by-char ( or element-by-element ???) comparison of the following block: from (starting) position (returned by position()
) inclusive to limit()
exclusive. Capacity and any content before position and after limit is not analyzed in both CharBuffer(s) by equals() method at all.
But phrases "same element type" and "...elements... pointwise" beat me.
Two char 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.
What else am I missing? What is element type? Isn't it always char inside CharBuffer?
P.S. According to implementation code (Java SE8) actually no (element) type check is done within equals(obj) - only standard check if (obj instanceof CharBuffer)
. So this phrase in API is just for some future implementation or for I-don't-know-what. Or maybe "same element type" just means that equals(arg) - arg must be CharBuffer, which is trivial.
Could you give an example of two CharBuffer objects with different "element type"?
Such CharBuffer objects are not equal by definition.
CharBuffer cb1 = CharBuffer.allocate(10);
cb1.put('0');
cb1.put('a'); // element type - char ?
cb1.put('b');
cb1.rewind();
cb1.limit(7);
System.out.println(cb1);
CharBuffer cb2 = CharBuffer.allocate(11);
cb2.put("0ab"); // element type - String ?
cb2.rewind();
cb2.limit(7);
System.out.println(cb2);
// 0ab = 0ab + same number of "empty positions" until limit
// (don't know how to name "empty positions" correctly)
System.out.println(cb1.equals(cb2)); // TRUE