Why do we need a magic number in the beginning of

2020-02-28 03:06发布

问题:

I read a few posts here about the magic number 0xCAFEBABE in the beginning of each java .class file and wanted to know why it is needed - what is the purpose of this marking?
Is it still needed anymore? or is it just for backwards compatibility now?

Couldn't find a post that answers this - nor did I see the answer in the java spec

回答1:

The magic number is basically an identifier for a file format. A JPEG for example always starts with FFD8. It is not necessary for Java itself, it simply helps to identify the file-type. You can read more about magic numbers here.



回答2:

See: http://www.artima.com/insidejvm/whyCAFEBABE.html

EDIT: and http://radio-weblogs.com/0100490/2003/01/28.html

Some answers:

Well, they presumably had to pick something as their magic number to identify class files, and there's a limit to how many Java or coffee related words you can come up with using just the letters A-F :-)

-

As to why the magic number is 3405691582 (0xCAFEBABE), well my guess is that (a) 32-bit magic numbers are easier to handle and more likely to be unique, and (b) the Java team wanted something with the Java-coffee metaphor, and since there's no 'J' or 'V' in hexadecimal, settled for something with CAFE in it. I guess they figured "CAFE BABE" was sexier than something like "A FAB CAFE" or "CAFE FACE", and definitely didn't like the implications of "CAFE A FAD" (or worse, "A BAD CAFE").

-

Don't know why I missed this before, but they could have used the number 12648430, if you choose to read the hex zeros as the letter 'O'. That gives you 0xC0FFEE, or 0x00C0FFEE to specify all 32 bits. OO COFFEE? Object Oriented, of course... :-)

-

I originally saw 0xCAFEBABE as a magic number used by NeXTSTEP. NX used "fat binaries", which were basically binaries for different platforms stuck together in one executable file. If you were running on NX Intel, it would run the Intel binary; if on HP, it would run the HP binary. 0xCAFEBABE was the magic number to distinguish either the Intel or the Motorola binaries ( can't remember which ).



回答3:

Magic numbers are a common technique to make things, such as files, identifiable.

The idea is that you just have to read the first few bytes of the file to know if this is most likely a Java class file or not. If the first bytes are not equal to the magic number, then you know for sure that it is not a valid Java class file.



回答4:

It's fairly common practice with binary files to have some sort of fixed identifier at the beginning (e.g. zip files begin with the characters PK). This reduces the possibility of accidentally trying to interpret the wrong sort of file as a class file.