What are the first four bytes of the .class file?

In the time of JDK 1.0/1.1 the was a long debate about different IDEs like Visual Cafe, JBuilder, Sun's Java Workshop, CodeWarrior etc. It was very similar to the today discussions about Ruby, .NET and Java :-). In every new project I was asked first about the different IDEs their advantages and drawbacks.
Once I was bored about the, not always constructive, discussions and said: "Because of the long compilation time, I write straight the byte code with UltraEdit in hex-mode".
You have just to remember the first bytes: "CAFEBABE" - everything else is easy.
No one expected such an answer - no one laughed. After two weeks the project managed came to me and asked my for a trainig about "Direct byte code programming" - he expected to save money on IDE-licenses :-). I was of course not able to give such a training and explained that...

But it is true: the first four bytes of every class-file are "CAFEBABE". If you change a letter - you will get a ClassFormatError - just try it out! It is actually waste of space - but cool :-)

Comments:

CAFEBABE is four bytes. That's eight nibbles.

Posted by Brian Duff on August 13, 2006 at 03:08 PM CEST #

Yes - you are right. I will change the post NOW :-)
Thank you!

Posted by Adam Bien on August 13, 2006 at 05:40 PM CEST #

I'm not sure I agree it's really a waste of space - it's a quick and easy way to make 80% sure the classloader is really looking at a .class file, not some text or other binary format.

See this discussion
http://en.wikipedia.org/wiki/File_format#Magic_number
for more information on magic numbers ...

Posted by Stefan Tilkov on August 13, 2006 at 06:27 PM CEST #

Post a Comment:
  • HTML Syntax: NOT allowed
...the last 150 posts
...the last 10 comments
License