简体   繁体   English

为什么在.class文件的开头需要一个魔术数字?

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

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 is needed - what is the purpose of this marking? 我在这里阅读了一些有关每个java .class文件开头的魔术数字0xCAFEBABE ,并想知道为什么需要它-此标记的目的是什么?
Is it still needed anymore? 还需要吗? or is it just for backwards compatibility now? 还是只是为了向后兼容?

Couldn't find a post that answers this - nor did I see the answer in the java spec 找不到可以回答这个问题的帖子-我也没有在Java 规范中看到答案

The magic number is basically an identifier for a file format. 幻数基本上是文件格式的标识符。 A JPEG for example always starts with FFD8. 例如,JPEG总是以FFD8开头。 It is not necessary for Java itself, it simply helps to identify the file-type. Java本身并不需要,它仅有助于识别文件类型。 You can read more about magic numbers here . 您可以在这里阅读更多有关幻数的信息

See: http://www.artima.com/insidejvm/whyCAFEBABE.html 请参阅: http//www.artima.com/insidejvm/whyCAFEBABE.html

EDIT: and http://radio-weblogs.com/0100490/2003/01/28.html 编辑:和http://radio-weblogs.com/0100490/2003/01/28.html

Some answers: 一些答案:

Well, they presumably had to pick something as their magic number to identify class files, and there's a limit to how many Java or coffee related words you can come up with using just the letters AF :-) 好吧,他们大概必须选择一些魔术数字来标识类文件,并且仅使用字母AF :-)就能想出多少个与Java或咖啡相关的单词是有限制的

- --

As to why the magic number is 3405691582 (0xCAFEBABE), well my guess is that (a) 32-bit magic numbers are easier to handle and more likely to be unique, and (b) the Java team wanted something with the Java-coffee metaphor, and since there's no 'J' or 'V' in hexadecimal, settled for something with CAFE in it. 关于为什么幻数是3405691582(0xCAFEBABE),我的猜测是(a)32位幻数更易于处理且更可能是唯一的,并且(b)Java团队想要Java咖啡。隐喻,并且由于十六进制中没有“ J”或“ V”,因此选择其中包含CAFE的内容。 I guess they figured "CAFE BABE" was sexier than something like "A FAB CAFE" or "CAFE FACE", and definitely didn't like the implications of "CAFE A FAD" (or worse, "A BAD CAFE"). 我猜他们认为“ CAFE BABE”比“ A FAB CAFE”或“ CAFE FACE”更性感,并且绝对不喜欢“ CAFE A FAD”(或更糟糕的是“ BAD CAFE”)的含义。

- --

Don't know why I missed this before, but they could have used the number 12648430, if you choose to read the hex zeros as the letter 'O'. 不知道为什么我以前错过了它,但是如果您选择将十六进制零作为字母'O'读取,他们可能会使用数字12648430。 That gives you 0xC0FFEE, or 0x00C0FFEE to specify all 32 bits. 这使您可以指定0xC0FFEE或0x00C0FFEE来指定所有32位。 OO COFFEE? 哦,咖啡? Object Oriented, of course... :-) 面向对象,当然... :-)

- --

I originally saw 0xCAFEBABE as a magic number used by NeXTSTEP. 我最初将0xCAFEBABE视为NeXTSTEP使用的幻数。 NX used "fat binaries", which were basically binaries for different platforms stuck together in one executable file. NX使用“胖二进制文件”,它们基本上是将不同平台上的二进制文件粘贴在一个可执行文件中。 If you were running on NX Intel, it would run the Intel binary; 如果您在NX Intel上运行,它将运行Intel二进制文件。 if on HP, it would run the HP binary. 如果在HP上,它将运行HP二进制文件。 0xCAFEBABE was the magic number to distinguish either the Intel or the Motorola binaries ( can't remember which ). 0xCAFEBABE是区分Intel或Motorola二进制文件(不记得哪个)的神奇数字。

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. 这个想法是,您只需要读取文件的前几个字节就可以知道这是否很可能是Java类文件。 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. 如果第一个字节不等于幻数,则可以确定它不是有效的Java类文件。

It's fairly common practice with binary files to have some sort of fixed identifier at the beginning (eg zip files begin with the characters PK). 对于二进制文件,在开头具有某种固定标识符是相当普遍的做法(例如,zip文件以字符PK开头)。 This reduces the possibility of accidentally trying to interpret the wrong sort of file as a class file. 这减少了意外尝试将错误类型的文件解释为类文件的可能性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM