Tuesday, January 27, 2009

what is CAFEBABE????


Yesterday when i was going through my classFiles folder which basically contains all of my .class files i found that some of the .java files(source programs) were missing so i was running all those .class files to which i didn't have the source file.....Meanwhile I got this thought as to what will happen if any of these files were not compiled and converted to a .class file by JVM...So i just opened text editor and typed some crap and saved it as ajay.class in my classFiles folder...
Now when i run this file i got an error which looks like "Exception in thread "main" java.lang.ClassFormatError: Incompatible magic value 218784115 in class file ajay"...Now according to the API the ClassFormatError is a part of java.lang package(can even be seen from the message printed through console object)and is Thrown "when the Java Virtual Machine attempts to read a class file and determines that the file is malformed or otherwise cannot be interpreted as a class file"...now the ClassFormatError is quite obvious but what's interesting are the last few words "magic value" what is it???? and the answer was quite interesting...Every class file which is compiler by JVM must have the first 4 bytes as "CAFEBABE" which bacisally is to tell the JVM that the class file is a valid one....i was more curious to know why it was 0xCAFEBABE and not something else......after doing little RnD i came up with a lot of stuff but none of them could make me 100% confident that i found the right answer,some of the answers were.....
  • some people say that its easier to represent in 32bits format
  • some say that there is no j or v in the hexadecimal so they came up with 'CAFEBABE'
Anyway one thing is per sure any class file has the fist 4 byte as 0xCAFEBABE.....
And its quite intersecting to know this fact isn't it!!!!!!!!
A tuitorial on how to use ecllipse..An IDE is coming up next
Till then enjoy the world of JAVA!!!!
AJAY:)

No comments:

Post a Comment