Java and entity

How does java care about content? In case you move the application from a small endian to a large content or vice versa. How are data elements or class properties affected?

+4
source share
3 answers

If you map a larger buffer over ByteBuffer , you can specify endianness using ByteOrder values. Old core libraries assume network order.

From ByteBuffer :

Binary access

This class defines methods for reading and writing the values ​​of all other primitive types except boolean. Primitive values ​​are translated into (or from) a sequence of bytes in accordance with the byte order of the current byte, which can be obtained and changed using order methods. Specific byte orders are represented by instances of the ByteOrder class. The initial byte buffer order is always BIG_ENDIAN.

and ByteOrder provides access to your own order for the platform on which you work.

Compare this to the old DataInput , which is not useful for interacting with local native services:

Reads four input bytes and returns an int value. Let ad be read from first to fourth bytes. Return value:

 (((a & 0xff) << 24) | ((b & 0xff) << 16) | ((c & 0xff) << 8) | (d & 0xff)) 
+3
source

The Java Virtual Machine abstracts this consideration, so you don’t have to worry about it while you are fully working in Java. The only reason you have to consider the byte order is to communicate with a process other than Java, or something like that.

Edit: edited for clarity

+8
source

The class file format is big-endian (or rather, all data with several bytes in the class file is stored this way):

Class file format

All 16-bit, 32-bit, and 64-bit values ​​are created by reading in two, four, and eight consecutive 8-bit bytes, respectively. Multibyte data elements are always stored in big-endian order, where high bytes are taken first.

But, as others have mentioned, as a practical consideration, you will hardly have to worry about this.

+2
source

Source: https://habr.com/ru/post/1385386/


All Articles