Code Pumpkin

What is magic number CAFEBABE in java class file?

November 29, 2017
Posted by Abhi Andhariya

Java Compiler compiles a java source code into bytecode and stores it into .class file which will then get executed by JVM. Everybody knows about it. But do you know each class file in Java starts with magic number CAFEBABE?  compelling, isn't it? 😀 

After reading this article, you will be able to answer below questions:

  1. What are magic numbers?
  2. Why is it required to start each class file with magic number CAFEBABE?
  3. How to Open class file in Hex format?
  4. What is the reason for choosing this specific word or number?

What are magic numbers?

In computers, file type can either be identified by extension ( e.g. .txt, .pdf) or some specific text 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, 

  • JPEG image files begin with FF D8 and end with FF D9
  • PDF files start with "%PDF" (hex 25 50 44 46).
  • Similarly, Compiled Java class files (bytecode) start with Hexadecimal number CAFEBABE.

Why is it required to start each class file with magic number CAFEBABE?

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 for getting your class file executed by any JVM. Any way, you don't need to worry about it, Java compiler will do that for you. 😀

How to Open class file in Hex format?

If you will try to open .class file directly in any text editor, it doesn't open in readable format. You need to have 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

HexEditor Plugin CAFEBABE 1

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

HexEditor Plugin CAFEBABE 2

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

HexEditor Plugin CAFEBABE 3

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

HexEditor Plugin CAFEBABE 4

What is the reason for choosing this specific Hexadecimal word CAFEBABE?

As per Alastair Mayer, there are two reasons.

  1. 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. 
  2. 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.

That's all for this topic. If you guys have any suggestions or queries, feel free to drop a comment. We would be happy to add that in our post. You can also contribute your articles by creating contributor account here.

Happy Learning 🙂

If you like the content on CodePumpkin and if you wish to do something for the community and the planet Earth, you can donate to our campaign for planting more trees at CodePumpkin Cauvery Calling Campaign.

We may not get time to plant a tree, but we can definitely donate ₹42 per Tree.

About the Author

Surviving Java Developer, Passionate Blogger, Table Tennis Lover, Bookworm, Occasional illustrator and a big fan of Joey Tribbiani, The Walking Dead and Game of Thrones...!!

Tags: , , , , ,

Comments and Queries

If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
<pre><code class="java"> 
String foo = "bar";
For more information on supported HTML tags in disqus comment, click here.
Total Posts : 124
follow us in feedly

Like Us On Facebook