Saturday, April 20, 2024
HomeJavaDistinction between ByteBuffer vs byte array in Java

Distinction between ByteBuffer vs byte array in Java


There are a number of variations between a byte array and ByteBuffer class in Java, however crucial of them is that bytes from byte array at all times reside in Java heap area, however bytes in a ByteBuffer might probably reside outdoors of the Java heap in case of direct byte buffer and reminiscence mapped information. Buffer is a byte array like abstraction which was launched in Java NIO launch to learn and write knowledge from FileChannel. It’s extensively utilized in Java NIO for transferring knowledge from one place to a different and its additionally a necessary Java ideas to know for any backend developer, notably those that needs to create non-blocking server software utilizing NIO in Java

Distinction between a Byte array and ByteBuffer in Java

What’s the distinction between a ByteBuffer and Byte array can also be a fashionable Java interview questions. Understanding and remembering the distinction may also assist you to do effectively on Java interviews. Listed here are a few extra key variations between byte arrays and ByteBuffer in Java:

1. Comparability utilizing equals()

You can’t examine byte array utilizing equals() and hashCode(), that you must use Arrays.equals() and Arrays.deepEquals() methodology however you’ll be able to examine two ByteBuffer utilizing these two strategies.

2. Byte Order (Massive Endian and Little Endian)

ByteBuffer permits you to learn and write knowledge in any byte order like each little-endian and big-endian. Massive Endian is also called community byte order.

Difference between byte array and ByteBuffer in Java

3. Copy

ByteBuffer has a singular functionality to go a subset of a byte buffer as a price with out copying the bytes, by instantiating a brand new ByteBuffer.

  • The NIO API makes in depth use of ByteBuffer:s.
  • The bytes in a ByteBuffer might probably reside outdoors of the Java heap.
  • A ByteBuffer has a state past the bytes themselves, which facilitates relative I/O operations (however with caveats, talked about beneath).
  • A ByteBuffer presents strategies for studying and writing numerous primitive varieties like integers and longs (and may do it in numerous byte orders).

4. Mutability

A byte array is a mutable in Java, which suggests its parts might be modified after creation however ByteByffer class is immutable in Java, though its content material might be modified

5. Writing

Byte array would not present any particular methodology to put in writing a very knowledge kind however ByteBuffer offers completely different strategies to put in writing completely different knowledge varieties into buffer like putInt(), putDouble(), putChar() and getInt(), getDouble() and getChar() strategies. There are numerous extra strategies to help all Java knowledge varieties.  

6. Capability

A byte array has a hard and fast capability which suggests you can’t change the capability of byte array as soon as created however ByteBuffer has dynamic capability and you may improve lower the capability after creation and relying upon knowledge.

Difference between byte array and ByteBuffer in Java

7. Concurrency and Thread security

Byte array just isn’t thread-safe therefore its not appropriate to be used in a multi-threaded and concurrent surroundings however ByteBuffer present thread secure entry  to its accessor strategies which makes it a more sensible choice in concurrent Java software.

8. Straightforward to work with Binary knowledge

Byte array would not present any utility methodology you’ll be able to simply save and get knowledge from index however ByteBuffer offers numerous strategies to cope with completely different knowledge varieties which makes it simpler to cope with binary knowledge .

9. Entry to Reminiscence Outdoors JVM

Many individuals would not know however you’ll be able to create direct byte buffer in Java which might signify reminiscence outdoors the heap, for instance MemoryMappedBuffer which is a subclass of DirectBuffer and supply entry to reminiscence mapped file in Java. 

10. Byte array and ByteBuffer Instance in Java

This is an instance of making and manipulating a byte array in Java:

byte[] byteArray = new byte[5];
byteArray[0] = 1
byteArray[1] = 2
byteArray[2] = 3
byteArray[3] = 4
byteArray[4] = 5

System.out.println("Byte Array: " + Arrays.toString(byteArray));

And this is an instance of making and manipulating a ByteBuffer in Java:

ByteBuffer byteBuffer = ByteBuffer.allocate(5);
byteBuffer.put((byte) 1)
byteBuffer.put((byte) 2)
byteBuffer.put((byte) 3)
byteBuffer.put((byte) 4)
byteBuffer.put((byte) 5)

System.out.println("Byte Buffer: " + Arrays.toString(byteBuffer.array()));

You may also use strategies like putInt() and putLong() to immediately put integer and lengthy in ByteBuffer in Java as proven beneath:

ByteBuffer buffer = ByteBuffer.allocate(100);
buffer.putInt(100);
buffer.putLong(200L);
buffer.flip();

System.out.println("Int worth: " + buffer.getInt());
System.out.println("Lengthy worth: " + buffer.getLong());

Right here is the output of the code, which shall be printed on Console:

Int worth: 100
Lengthy worth: 200

That is all in regards to the distinction between ByteBuffer and Byte array in Java. You should utilize ByteBuffer whenever you use FileChannel to learn knowledge from a Socket or File in Java and byte array for regular IO and coping with InputStream and OutputStream. Channel additionally permits you to carry out non-blocking learn in Java. 

Different Java IO tutorials you might like:

  • Easy methods to learn CSV information in Java? (program)
  • Easy methods to convert ByteBuffer to String in Java? (instance)
  • Easy methods to append knowledge into an present file in Java? (reply)
  • Easy methods to learn the file in a single line in Java 8? (answer)
  • Easy methods to Repair java.lang.OufOfMemoryError: Direct Buffer Reminiscence in Java (answer)
  • Easy methods to learn the file line by line in Java utilizing BufferedReader and Scanner? (reply)
  • Easy methods to learn/write an Excel file in Java? (program)

Thanks for studying this text thus far. For those who like this Java
ByteBuffer vs Array Tutorial  then please share with your pals and
colleagues. If in case you have any questions or suggestions, please ask. 



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments