Adam Bien's Weblog

Sunday Aug 13, 2006

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 :-)


[my tweets]  Rss My book: Real World Java EE - Rethinking Best Practices

Kommentare:

CAFEBABE is four bytes. That's eight nibbles.

Gesendet von Brian Duff am August 13, 2006 at 01:08 PM CEST #

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

Gesendet von Adam Bien am August 13, 2006 at 03: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 ...

Gesendet von Stefan Tilkov am August 13, 2006 at 04:27 PM CEST #

Senden Sie einen Kommentar:
  • HTML Syntax: Ausgeschaltet
Meta-stuff / Interviews
My Recent Book
Java One 2009
CommunityOne East N.Y.C
JavaONE 2008 Interview
Search
...the last 150 posts
...the last 10 comments
greenfire.dev.java.net
Links
my.netbeans.org
Visitors
License