Why do we need a magic number in the beginning of the .class file?

前端 未结 4 1080
陌清茗
陌清茗 2021-01-01 23:58

I read a few posts here about the magic number 0xCAFEBABE in the beginning of each java .class file and wanted to know why it

4条回答
  •  余生分开走
    2021-01-02 00:32

    Magic numbers are a common technique to make things, such as files, identifiable.

    The idea is that you just have to read the first few bytes of the file to know if this is most likely a Java class file or not. If the first bytes are not equal to the magic number, then you know for sure that it is not a valid Java class file.

提交回复
热议问题