Sunday 7 November 2021

Java: Get native byte order of the underlying platform

ByteOrder.nativeOrder() method return the native byte order of the hardware upon which this Java virtual machine is running. This is different from jvm byte order.

 

As per the below stackoverflow post, JVM follows big-endian format irrespective of the underlying platform byte order.

https://stackoverflow.com/questions/362384/does-java-read-integers-in-little-endian-or-big-endian

 

NativeByteOrder.java

package com.sample.app.numbers;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

public class NativeByteOrder {

	public static void main(String args[]) {
		System.out.println("Native system byte order " + ByteOrder.nativeOrder());

		ByteBuffer bb = ByteBuffer.allocate(2);
		System.out.println("JVM Default Byte Order: " + bb.order());
	}

}

 

Output

Native system byte order LITTLE_ENDIAN
JVM Default Byte Order: BIG_ENDIAN

  

You may like

No comments:

Post a Comment