Disclosure: This article may contain affiliate links. When you purchase, we may earn a small commission.

Difference between ByteBuffer vs byte array in Java

There are several differences between a byte array and ByteBuffer class in Java, but the most important of them is that bytes from byte array always reside in Java heap space, but bytes in a ByteBuffer may potentially reside outside of the Java heap in case of direct byte buffer and memory mapped files. Buffer is a byte array like abstraction which was introduced in Java NIO release to read and write data from FileChannel. It is extensively used in Java NIO for transferring data from one place to another and its also an essential Java concepts to know for any backend developer, particularly those who wants to create non-blocking server application using NIO in Java

Difference between a byte array and byte buffer in Java

What is the difference between a ByteBuffer and Byte array is also a popular Java interview questions. Knowing and remembering the difference can also help you to do well on Java interviews. Here are a couple of more key differences between byte arrays and ByteBuffer in Java:

1. Comparison using equals()

You cannot compare byte array using equals() and hashCode(), you need to use Arrays.equals() and Arrays.deepEquals() method but you can compare two ByteBuffer using those two methods.

2. Byte Order (Big Endian and Little Endian)

ByteBuffer allows you to read and write data in any byte order like both little-endian and big-endian. Big Endian is also known as network byte order.

Difference between byte array and ByteBuffer in Java

3. Copy

ByteBuffer has a unique capability to pass a subset of a byte buffer as a value without copying the bytes, by instantiating a new ByteBuffer.
  • The NIO API makes extensive use of ByteBuffer:s.
  • The bytes in a ByteBuffer may potentially reside outside of the Java heap.
  • A ByteBuffer has a state beyond the bytes themselves, which facilitates relative I/O operations (but with caveats, talked about below).
  • A ByteBuffer offers methods for reading and writing various primitive types like integers and longs (and can do it in different byte orders).

That's all about the difference between ByteBuffer and Byte array in Java. You can use ByteBuffer when you use FileChannel to read data from a Socket or File in Java and byte array for normal IO and dealing with InputStream and OutputStream. Channel also allows you to perform non-blocking read in Java. 

Other Java IO tutorials you may like:
  • How to read CSV files in Java? (program)
  • How to convert ByteBuffer to String in Java? (example)
  • How to append data into an existing file in Java? (answer)
  • How to read the file in one line in Java 8? (solution)
  • How to Fix java.lang.OufOfMemoryError: Direct Buffer Memory in Java (solution)
  • How to read the file line by line in Java using BufferedReader and Scanner? (answer)
  • How to read/write an Excel file in Java? (program)

Thanks for reading this article so far. If you like this Java ByteBuffer vs Array Tutorial  then please share with your friends and colleagues. If you have any questions or feedback, please ask. 

1 comment :

Anonymous said...

Nice tutorial

Post a Comment