Tuesday, January 28, 2003


Recently, the origin of the magic numbers of 0xCAFEBABE and 0xFEEDFACE came up in an email conversation between a number of ex-NeXT folks.  Specifically, someone asked what the origin of the two magic numbers were in the context of mach-o file formats and why had Java chosen to also use 0xCAFEBABE as the .class file magic number?

As I have been a NeXT user since 1989 and a Java user since the day Sun released the original JDK source (which I helped port to the NeXT), I thought I could intelligently answer said question and had enough documentation to back me up.

My response:

A random thread... but not totally relevant.

JavaLobby: Why CAFEBABE?

Back to reality -- seems Java and Mac Classic people like to invent history.   The following is excerpted from the /etc/magic file on an OS X machine.   Mach-o files can support multiple architectures in a single binary... so, both 0xfeedface and 0xcafebabe denote mach-o files and are not architecture specific.

As to why the Java folks chose the same magic number, it is all an ex-NeXT'ers fault...   This is copied directly out of /etc/magic on my OS X system (and appears on basically every other unix distribution, as well):

# Java ByteCode
# From Larry Schwimmer (schwim@cs.stanford.edu)
# The Java magic number is the same as the Mach-O fat file magic number.
# Interestingly the the same guy, Mike DeMoney, of NeXT Inc. then of First
# Person (Sun) picked both numbers only a couple of years apart.  To make
# the file(1) command work with both the Java Bytecode magic was merged with
# the Mach-O magic in the file mach.


Mike DeMoney happened to be one of the recipients of the above message and he responded with the following message.

This would be mildly interesting, if true.

The simple fact is that while I left NeXT to go work at FirstPerson,
(the birthplace of Java within Sun) I had nothing to do with choosing
the magic numbers for Java -- they were chosen well before I arrived.
I may have chosen the numbers for the NeXT mach-o's (I honestly don't
remember if I did), since I did do the Mach kernel work for the "fat"
binary stuff while I was at NeXT.

I believe this story was possibly started by a friend at NeXT (and now Apple
and who shall remain nameless -- he can speak up if he wishes) who
used to kid me about this -- I think the kidding took on a unwarranted
life of its own.  Or maybe he really assumed that since I worked on Mach-o's
and went to the Java team I must have been the conduit for this similarity.
It's a nice story, again it's just not true.

I think the truth of this story is that there are only a limited number
of "cute" words you can spell with hex characters, so the likelyhood
of two independent groups picking the same ones isn't all that surprising.
(What's surprising is that more folks haven't figured that out....)
So if anything, you can blame me for choosing CAFEBABE at NeXT -- which
in retrospect isn't a very good choice; but I didn't do it again for Java.

But that's not as interesting.  I have no idea who Larry Schwimmer is,
he's certainly never spoken to me.

Mike DeMoney

Definitely a case of Computer history is written by those with CVS commit rights...   A followup response from someone else stated:

Cafe babe was a specific barista at Pete's Coffee at Homer & High in Palo Alto.

When Java was Oak, and FirstPerson was the Green Project the magic number was chosen by James and other aficionados of fine coffee.

I remember the discussion.

OK -- this is interesting and almost definitive.  If you want an answer, go to the ultimate source.  To gain a definitive answer, I forwarded the whole thread to James Gosling and asked for his input.   James kindly responded with the full story:

As far as I know, I'm the guilty party on this one.  I was totally unaware of the NeXT connection.  The small number of interesting HEX words is probably the source of the match.  As for the derivation of the use of CAFEBABE in Java, it's somewhat circuitous:

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.

So, there you have it... 

I would be interested to hear how Larry Schwimmer came up with the information that he wrote in /etc/magic.  

A hearty thanks to James, Mike, and others for taking the time to contribute detailed responses and for giving permission for me to include their responses here.  I find history of this nature to be thoroughly amusing and interesting.

Update: Eric wrote to mention that Larry [who I mistakenly called David - fixed] is now at Google.
10:58:22 AM