Java byte, host byte, and network byte literacy stickers

Source: Internet
Author: User

Java programmers are happy, because compared with C/C ++'s non-cross-platform architecture, JVM shields us from a lot of underlying details and complexity, this allows us to focus on implementing specific business logic, so the efficiency of developing projects using java is relatively high. At the same time, java programmers are sad because JVM shields a lot of technical details, which leads to a general poor basic skills for java Programmers. They have no deep understanding of some basic concepts and have never even heard of them. As a java programmer, I am deeply aware of my narrow knowledge. I accidentally saw the byte order, but I didn't even know it before. I recorded it here and scanned it for blindness.

Programmers who use C/C ++ for network programming will certainly be exposed to the concept of "byte sequence", but those who use java for network programming will not be exposed to "byte sequence" at all ". Why? Let's start with the byte sequence. Byte sequence refers to the storage order of data that occupies more than one byte in the memory. There are two types of sequence: Small-end and large-end. Little endian: Low-byte data is stored in the low-address area of the memory, and high-byte data is stored in the high-address area. bigendian ): high-byte data is stored at the low address, and low-byte data is stored at the high address.

In java, an int type data occupies 4 bytes. Assume that there is a hexadecimal int number, int value = 0x01020304. Different bytes are used, for details about the storage in the memory, see:


Obviously, the large byte order is more in line with human thinking habits.

As to whether the computer is BIG-ENDIAN, LITTLE-ENDIAN, CPU-related, a CPU is not a BIG-ENDIAN or LITTLE-ENDIAN. In the IA architecture (Intel and AMD), the CPU is Little-Endian, while in PowerPC, iSCSI, and MotZ memory? Http://www.bkjia.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> keys/b7d1NrN + MLnyc + keys/u6z8jLwOC1xM + wud + keys + authorization + ajrNT1w7TF0LbPxOO1xLzGy + O7 + authorization + zb/J0tTWqrXAu/rG97XE19a92tDyPC9wPgo8cD48L3A + CjxwcmUgY2xhc3M9 "brush: java; "> System. out. println (ByteOrder. nativeOrder ());The ByteOrder and ByteBuffer classes are provided in the java. nio package. We can also change the default byte order in the JVM. This example is from

Http://blog.csdn.net/veryitman/article/details/6819017

The Code is as follows:

Package net. aty. util; 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 );}}

The execution result is as follows:

Default byte order BIG_ENDIAN, memory data [1, 2, 3, 4]
Modify the byte order LITTLE_ENDIAN, memory data [4, 3, 2, 1]

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.