Magic Number (CAFEBABE) for Java Class Files

Every Java Developer knows about it, Java Compiler compiles a java source code into bytecode and stores it into .class file which will then get executed by JVM. But do you know all class file in Java starts with Magic Number CAFEBABE hash code?

Magic Number CAFEBABE in Java Class File

What are Magic Numbers?

In computer systems, file type can either be identified by extension ( e.g. .jpeg, .txt, .pdf) or some specific text i.e. Magic Number in the starting content of each file.

Magic numbers are constant numbers or text value which is used to identify the file format. Detecting such constants in files is a simple and effective way of distinguishing between many file formats and can yield further run-time information. For Example,

  • PDF files start with “%PDF” (hex 25 50 44 46).
  • JPEG image files begin with FF D8 and end with FF D9

Similarly, Java compiled class files (bytecode) start with Hexadecimal number CAFEBABE.

Why Magic Number CAFEBABE is required to start each Class File?

Well, the file has to start with some magic number or string (e.g. CAFEBABE) to make it easy to recognize as a valid class file and not something else.

In other words, JVM will only read and executes .class files which starts with hex number CAFEBABE and it skips rest of the files. So, CAFEBABE is must as first line for getting your class file executed by any JVM. Anyway Java compiler will take care of it while converting to byte code.

What is history to choose Magic Number CAFEBABE for Java?

As per Alastair Mayer, there are two reasons.

32-bit magic numbers (CA FE BA BE i.e. 1 byte for each pair) are easier to handle and more likely to be unique. 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. “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”)

However, James Gosling, the father of Java programming language, once explained it as follows:

We used to go to lunch at a place called St Michael’s Alley. According to local legend, in the deep dark past, the Grateful Dead used to perform there before they made it big. It was a pretty funky place that was definitely a Grateful Dead Kinda Place. When Jerry died, they even put up a little Buddhist-esque shrine. When we used to go there, we referred to the place as Cafe Dead.

Somewhere along the line it was noticed that this was a HEX number. I was re-vamping some file format code and needed a couple of magic numbers: one for the persistent object file, and one for classes. I used CAFEDEAD for the object file format, and in grepping for 4 character hex words that fit after CAFE (it seemed to be a good theme) I hit on BABE and decided to use it.

At that time, it didn’t seem terribly important or destined to go anywhere but the trash-can of history. So CAFEBABE became the class file format, and CAFEDEAD was the persistent object format. But the persistent object facility went away, and along with it went the use of CAFEDEAD – it was eventually replaced by RMI.

How to Open Class File In Hex Format?

To open .class file directly in any text editor, it doesn’t open in readable format. You required editor which supports opening file in Hex format. You can use notepad++ for that purpose. However, you need to install HEX-Editor plugin. Here are the steps for the same :

1) Download and install Notepad++ from here.

2) After opening Notepad++, Go to Plugins -> Plugin Manager -> Show Plugin Manager

Notepad ++ HexEditor Plugin

3) Search and check HEX-Editor Plugin. Click Install Button.

Notepad ++ HexEditor Plugin

4) Restart Notepad++. Go to Plugins -> HEX-Editor -> View in HEX

Notepad ++ HexEditor Plugin

5) That’s it. Open your class file and you can see ca fe ba be in the first line of it.

Notepad ++ HexEditor Plugin


Related Posts

Your Feedback Motivate Us

If our FacingIssuesOnIT Experts solutions guide you to resolve your issues and improve your knowledge. Please share your comments, like and subscribe to get notifications for our posts.

Happy Learning !!!