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

RonK picture RonK · Apr 26, 2011 · Viewed 7.2k times · Source

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

Answer

Demento picture Demento · Apr 26, 2011

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.