0

I just interested about the difference between these two guys ByteBuffer.allocateDirect(4) and ByteBuffer.allocate(4)

and why when I call the array() method of direct one, it throws exception? what is the difference? would you provide a simple example and usage too? many thanks.

1 Answers1

0

When I call the array() method of direct one, it throws exception? What is the difference?

A HeapByteBuffer (from allocate()), is backed by an array, but a DirectByteBuffer (from allocateDirect()) is backed by an block of unmanaged memory, which it accesses directly. It has no backing array, and thus calling array() will throw.

The question @Vakh links to in his comment explains the differences nicely.

Mike Strobel
  • 25,075
  • 57
  • 69