Java_network_java byte order

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.