Nilesh Ghule’s Post

View profile for Nilesh Ghule, graphic

Technical Director | Big Data | Java Full Stack | Embedded | System | Training

The magic number of Java's .class file is 0xCAFEBABE (hex). Yesterday I was teaching JVM architecture. Telling the magic number of the .class file is simple, but explaining the concept of magic number is a bit tricky (it's not Java). First of all, the magic number concept is not limited to Java or executable files. Almost all binary files have a magic number that identifies the file format. Typically first 2 or 4 bytes of any file is a unique byte sequence called a magic number. It is also referred to as file signature. When a file is created, the writer program adds the magic number to the file; while reading the file the reader program verifies the magic number. If the magic number is correct, then only the rest of the file is read and processed. Example: 1. The Java compiler adds the magic number to the .class file and JVM verifies it before executing it (during the class loading process). 2. For C/C++ programs the Linker adds the magic number into an executable file and the OS loader verifies it while loading the program for execution. 3. An audio recorder adds the magic number to the .mp3 file. An audio player checks if the magic number is right and then renders the audio data in it. Here is a list of important magic numbers. Do refer to Wikipedia for magic numbers of other files. https://lnkd.in/dcgdPqnr Have I missed anything important? Please do add in comments. #java #operatingsystems #nileshcoretalks

  • No alternative text description for this image
Shubham More

Silicon Validation Engineer at Ampere computing.

8mo

Good morning sir, Few questions I have : 1. How this magic numbers are generated? Are this magic numbers fixed for a file? 2. As mentioned in c/c++ the linker loads the magic numbers after that loader verifies it, but who passes this magic numbers to loader? 3. If magic number didn't match while reading a file, does it generates any exception?

Thanks Nilesh Ghule sir for sharing this.

See more comments

To view or add a comment, sign in

Explore topics