3

I cannot understand particular details of CharBuffer equals() method functioning.

I don't understand this "considered independently of their starting positions" phrase:

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.

I studies these good examples - more examples, but I don't understand the idea.

Could anyone explain in different words and with minimal insightful example code?

Particularly I find this strange:

CharBuffer cb1 = CharBuffer.allocate(10);
cb1.put('a');
cb1.put('b');
//cb1.rewind();
System.out.println(cb1);


CharBuffer cb2 = CharBuffer.allocate(10);
cb2.put(4,'a');
cb2.put(5,'b');
//cb2.rewind();
System.out.println(cb2);

// false, uncommenting rewind() - also false
// but shall be true - "considered independently of starting positions" ?
System.out.println(cb1.equals(cb2));  
Code Complete
  • 3,146
  • 1
  • 15
  • 38

3 Answers3

3

The two sequences of remaining elements, considered independently of their starting positions, are pointwise equal.

  1. Only looking at the remaining elements,
  2. Without considering where they started,
  3. Each element of one buffer must be equal to the corresponding element in the other buffer.

In your example, it is the pointwise equal part that is important:

      0 1 2 3 4 5 6 7 8 9
cb1 = a b 0 0 0 0 0 0 0 0
cb1 = 0 0 0 0 a b 0 0 0 0

As you can see, when comparing the elements of the char buffer pointwise, they do not match up.

Billy Brown
  • 2,272
  • 23
  • 25
2

The CharBuffer are compared by what is their remaining content. It means that the equals() check starts from the current buffer position and not from the buffer start. As per Buffer.position():

A buffer's position is the index of the next element to be read or written. A buffer's position is never negative and is never greater than its limit.

Some methods like put(char) will change the buffer position:

CharBuffer cb1 = CharBuffer.allocate(10);
cb1.put('a'); // increments position
cb1.put('b'); // increments position

CharBuffer cb2 = CharBuffer.allocate(8);

System.out.println(cb1.equals(cb2)); // true, 00000000 equals 00000000

In your example after cb1.rewind() the first buffer is ab00000000 while the second buffer is 0000ab0000. Calling cb2.rewind() is not needed as put(char, int) doesn't change the buffer position:

CharBuffer cb1 = CharBuffer.allocate(10);
cb1.put((char) 0);
cb1.put((char) 0);
cb1.put((char) 0);
cb1.put((char) 0);
cb1.put('a');
cb1.put('b');
// put(char) increments position so we need to rewind
cb1.rewind(); 

CharBuffer cb2 = CharBuffer.allocate(10);
cb2.put(4, 'a');
cb2.put(5, 'b');

System.out.println(cb1.equals(cb2)); // true, 0000ab0000 equals 0000ab0000
Karol Dowbecki
  • 43,645
  • 9
  • 78
  • 111
0

Here's an example that returns true for two buffers of differing sizes:

CharBuffer cb1 = CharBuffer.allocate(8);
cb1.put('x'); // moves the current position to 1
cb1.put('x'); // moves the current position to 2
cb1.put(2,'a');
cb1.put(3,'b');
System.out.print(cb1);System.out.println ('|');
CharBuffer cb2 = CharBuffer.allocate(10);
cb2.get (); // moves the current position to 1
cb2.get (); // moves the current position to 2
cb2.get (); // moves the current position to 3
cb2.get (); // moves the current position to 4
cb2.put(4,'a');
cb2.put(5,'b');
System.out.print(cb2);System.out.println ('|');
System.out.println(cb1.equals(cb2));

equals compares the elements in positions 2 to 7 of cb1 to the elements in positions 4 to 9 of cb2, and finds that they are pair-wise equal (both contain the following chars - 'a','b',0,0,0,0).

You can see that the starting position is different in the two buffers (2 vs. 4), but the sequences of remaining elements are the same.

     0   1   2   3  4   5  6 7 8 9

cb1 'x' 'x' 'a' 'b' 0   0  0 0
             ^
cb2  0   0   0   0 'a' 'b' 0 0 0 0
                    ^

When you start comparing the sequences from the starting position, you get two identical sequences.

In your example, you are comparing the sequence 0,0,0,0,0,0,0,0 (the sequence of remaining elements starts at position 2) to the sequence 0,0,0,0,'a','b',0,0,0,0 (the sequence of remaining elements starts at position 0). Clearly, they are not equal to each other.

Eran
  • 387,369
  • 54
  • 702
  • 768