Why 0xCAFEBABE: the story behind Java's class-file magic number

Translated by a model; the original is in Russian.

· Curiosities
Every Java class file begins with the four bytes 0xCAFEBABE. In 1996 programmer Bill Venners wondered where the value came from — and got an answer from one of the language's creators.

cover.svg

The first four bytes of any Java .class file are the so-called magic number 0xCAFEBABE. It lets tools quickly tell class files apart from everything else. In May 1996, while working on an article about the class-file lifecycle, Bill Venners wondered whether the value carried any deeper meaning and posed the question on the comp.lang.java newsgroup.

Theories and guesses

The replies were rich in speculation. One poster reasonably noted that some magic number had to be picked anyway, and there are only so many coffee-themed words you can spell with the hex digits A–F. Another figured "CAFE BABE" simply sounded catchier than "A FAB CAFE" or "CAFE FACE" — and certainly better than "A BAD CAFE." Someone else suggested reading the zeros as the letter O to get 0x00C0FFEE — "OO COFFEE," object-oriented coffee, of course.

Predecessors were recalled too. The Burroughs B6700 once filled its "uninitialized" memory with words of 0xBADBADBADBAD. The classic placeholder 0xDEADBEEF is still around; reading uninitialized memory is called a "dead beef error" in some circles. There was also a theory that Sun borrowed 0xCAFEBABE from NeXTSTEP, where the value marked "fat binaries" targeting different processors.

The answer from the source

The real answer came from Patrick Naughton, one of the team members who eventually produced Java. He explained that the magic number for class files was chosen long before the language was ever called Java. The team wanted something fun, unique and easy to remember; 0xCAFEBABE beat the runner-up, 0xDEADBABE. That the number turned out to be an oblique nod to the cute baristas at Peet's Coffee — and seemed to foreshadow the name Java — was pure coincidence.

The rejected candidate proved fortunate to skip: 0xDEADBABE turned out to sit at the start of every IRIX arena file. And a debugging Solaris kernel, for its part, stamped freed memory blocks with 0xCAFEFEED.

The episode shows that behind one of Java's most recognizable artifacts lies not some clever design but engineers simply wanting to pick something amusing and memorable.


Source: Lobsters

Comments

Sign in, to comment.

  • No comments yet.