In the previous two blogs, we introduced byte and network byte.
If you are interested, you can access several functions related to java_network_endianness-byte order and java_network_endian.
This blog mainly talks about JavaVM and byte sequence. It is explained through an example.
We know that the mechanism for storing data in memory varies with different CPUs or different operating systems with the same CPU!
But how does the Java Virtual Machine store data? In other words, is the JVM large-end or small-End sequence? For example!
Package mark. zhang; import Java. NIO. bytebuffer; import Java. util. arrays; public class jvmendiantest {public static void main (string [] ARGs) {int x = 0x01020304; bytebuffer BB = bytebuffer. wrap (New byte [4]); bb. asintbuffer (). put (x); string ss_before = arrays. tostring (BB. array (); system. out. println ("Default byte order" + BB. order (). tostring () + "," + "Memory Data" + ss_before );}}
Run this demo and the result is as follows:
Default byte order big_endian, memory data [1, 2, 3, 4]
It can be seen that the JVM's byte order is the large-end order. Can the JVM's byte order be changed? Let's take a look at the example below to answer this question.
In this example, we only use the bytebuffer class method to illustrate the problem.
Package mark. zhang; import Java. NIO. bytebuffer; import Java. NIO. byteorder; import Java. util. arrays; public class jvmendiantest {public static void main (string [] ARGs) {int x = 0x01020304; bytebuffer BB = bytebuffer. wrap (New byte [4]); bb. asintbuffer (). put (x); string ss_before = arrays. tostring (BB. array (); system. out. println ("Default byte order" + BB. order (). tostring () + "," + "Memory Data" + ss_before); bb. order (byteorder. little_endian); bb. asintbuffer (). put (x); string ss_after = arrays. tostring (BB. array (); system. out. println ("Modify byte order" + BB. order (). tostring () + "," + "Memory Data" + ss_after );}}
Run as javaapp output result:
Default byte order big_endian, memory data [1, 2, 3, 4] modifying byte order little_endian, memory data [4, 3, 2, 1]
OK. Let's talk about it here!
If you are interested, you can study the bytebuffer class.