Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we need a magic number in the beginning of the .class file?

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

like image 825
RonK Avatar asked Apr 26 '11 21:04

RonK


1 Answers

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 ).

like image 120
magma Avatar answered Sep 24 '22 22:09

magma