What is magic number CAFEBABE in java class file?
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:
- What are magic numbers?
- Why is it required to start each class file with magic number CAFEBABE?
- How to Open class file in Hex format?
- What is the reason for choosing this specific word or number?
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
D8and end with
PDF files start with "%PDF" (hex
- Similarly, Compiled Java class files (bytecode) start with Hexadecimal 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. 😀
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
3) Seach and check HEX-Editor Plugin. Click Install Button.
4) Restart Notepad++. Go to Plugins -> HEX-Editor -> View in HEX
5) That's it. Open your class file and you can see ca fe ba be in the first line of it.
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
CAFEDEADfor 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
BABEand 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
CAFEBABEbecame the class file format, and
CAFEDEADwas 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.
Comments and QueriesIf 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"; </code></pre>For more information on supported HTML tags in disqus comment, click here.