I'm having difficulty understanding the semantics of ByteBuffer
in the following scenario:
int sizeOfDouble = 8;
int numberOfDoubles = 4;
ByteBuffer testBuf = ByteBuffer.allocateDirect(sizeOfDouble*numberOfDoubles);
testBuf.putDouble(0, 1.0);
testBuf.putDouble(1, 2.0);
testBuf.putDouble(2, 3.0);
testBuf.putDouble(3, 4.0);
for (int i = 0; i < numberOfDoubles; ++i) {
System.out.println("testBuf[" + i + "]: " + testBuf.getDouble(i));
}
I expected to see the values I had just put into the ByteBuffer
to be printed to the screen. Instead, I get this output:
testBuf[0]: 4.959404759574682E-4
testBuf[1]: 32.50048828125
testBuf[2]: 32.125
testBuf[3]: 4.0
The value at the third index seems to match what I expect: 4.0. But why aren't the values and indices 0, 1, and 2 matching up with the values I inserted (1.0, 2.0, and 3.0, respectively)?
I suspect I misunderstand something about how ByteBuffer
works, but I haven't been able to find it in the javadoc.