java.class-file

Usage of 0xcafebabe in Java class file to identify byte order?


Is 0xcafebabe of the Java class file being used to identify the byte order e.g. in a network class loader? A java class could be serialized and sent over the network to another machine to be remote executed by being loaded via a network class loader. In such a scenario, is 0xcafebabe used to see if it is "0xbabecafe" or "0xcafebabe"?

I suppose the answer is No but to be sure. I think the byte order in a .class file is always in big endian, and I always see 0xcafe at the top in the file system on any machines. When a class is serialized, I suppose it is always in big endian (correct?). When .class or a serialized class is sent over the network byte-by-byte, the endianness is preserved. Hence we do not have to look after the byte order by ourselves.

Please advise if this is correct, or correct if there are cases when this is not correct. And 0xcafebabe is merely a marker to tell "this file can be a java class file".


Solution

  • Your answer is here:

    Multibyte data items are always stored in big-endian order, where the high bytes come first.

    The same chapter details the class file structure, including the magic cafebabe.

    How this file would be transferred over a network depends on the protocol used. For example TCP uses big endianness. But this is somewhat irrelevant because your would expect to receive the same file at the other end, regardless of how bytes are sequenced during transfer.