Java Magic. Part 2: 0xCAFEBABE
Java Magic. Part 2: 0xCAFEBABE
@(Base)[JDK, magic, 黑魔法]
转载请写明:原文地址
英文原文
系列文章:
-Java Magic. Part 1: java.net.URL
-Java Magic. Part 2: 0xCAFEBABE
-Java Magic. Part 3: Finally
-Java Magic. Part 4: sun.misc.Unsafe
你知道所有的java class文件都有一个相同的4字节串吗。这个4字节串的16进制是CAFEBABE。
我们可以简单编写一个Hello.java来验证一下:
public class Hello {
public static void main(String[] args) {
System.out.println("Hell, O'World!");
}
}
我们用javac编译该文件。然后用Emacs打开这个class文件(使用M-x hexl-mode)来查看,我们得到如下信息:
00000000: cafe babe 0000 0033 001d 0a00 0600 0f09 .......3........
00000010: 0010 0011 0800 120a 0013 0014 0700 1507 ................
00000020: 0016 0100 063c 696e 6974 3e01 0003 2829 .....<init>...()
00000030: 5601 0004 436f 6465 0100 0f4c 696e 654e V...Code...LineN
00000040: 756d 6265 7254 6162 6c65 0100 046d 6169 umberTable...mai
00000050: 6e01 0016 285b 4c6a 6176 612f 6c61 6e67 n...([Ljava/lang
// others..
根据白皮书的规范,前四个字节(u4)cafebabe就是Class文件的魔数,第5、6字节(u2)是Class文件的次版本号,第7、8字节(u2)是主版本号。十六进制0和33,也就是版本号为51.0。
有趣的就是这个4字节魔数,根据James Gosling的博客,他是这么解释的:
We used to go to lunch at a place called St Michael's Alley. According to local legend, in the deep dark past, the Grateful Dead used to perform there before they made it big. It was a pretty funky place that was definitely a Grateful Dead Kinda Place. When Jerry died, they even put up a little Buddhist-esque shrine. When we used to go there, we referred to the place as Cafe Dead. Somewhere along the line it was noticed that this was a HEX number. I was re-vamping some file format code and needed a couple of magic numbers: one for the persistent object file, and one for classes. I used CAFEDEAD for the object file format, and in grepping for 4 character hex words that fit after "CAFE" (it seemed to be a good theme) I hit on BABE and decided to use it. At that time, it didn't seem terribly important or destined to go anywhere but the trash-can of history. So CAFEBABE became the class file format, and CAFEDEAD was the persistent object format. But the persistent object facility went away, and along with it went the use of CAFEDEAD - it was eventually replaced by RMI.
以前我们曾去过一个叫StMichael's Alley的地方吃中午饭。根据当地一个传说,在很久很久以前,一个叫做Grateful Dead的乐队在成名之前一直都在这里表演。当乐队成员Jerry死了之后,这里还一度变成纪念他的地方。我注意到这个地方写着一串16进制的魔术(这尼玛明明是个咖啡馆的名字好吗)。当时我正在改造一些文件的格式,我当时需要2个魔术:一个是用来表示object file,另外一个用来表示class文件。我选择使用CAFEDEAD来表示object file,然后把后面2个字节换掉之后用来表示class文件。我突然想到BABE(小孩的意思)这个放在CAFE的后面应该不错。当时看来这个东西好像并不是特别重要啦~所以,结果就是你们现在看到的CAFEDEAD用来表示持久化的object format(也就是object序列化之后的结果),然后CAFEBABE就用来表示class文件。当时persistence object的最终被RMI(java远程调用)替换掉了,CAFEDEAD也就不复存在了。
0xCAFEBABE的十进制是3405691582。如果我们把所有位加起来得到43。恰好大于42-Ultimate Answer to the Life, the Universe, and Everything。另外43也是一个质数。You see, magic everywhere. Even in the last sentence.
这个作者脑子有点毛病..
Java Magic. Part 2: 0xCAFEBABE的更多相关文章
- Java Magic. Part 4: sun.misc.Unsafe
Java Magic. Part 4: sun.misc.Unsafe @(Base)[JDK, Unsafe, magic, 黑魔法] 转载请写明:原文地址 系列文章: -Java Magic. P ...
- Java Magic. Part 3: Finally
Java Magic. Part 3: Finally @(Base)[JDK, magic, 黑魔法] 转载请写明:原文地址 英文原文 系列文章: -Java Magic. Part 1: java ...
- Java Magic. Part 1: java.net.URL
Java Magic. Part 1: java.net.URL @(Base)[JDK, url, magic, 黑魔法] 英文原文 转载请写明:原文地址 系列文章: -Java Magic. Pa ...
- Magic Cast Method in Java Magic Trick In Java
https://www.atlassian.com/blog/archives/magic_trick_in_java https://www.gamlor.info/wordpress/2010/1 ...
- Java 动态代理
被代理的接口特点: 1. 不能有重复的接口,以避免动态代理类代码生成时的编译错误. 2. 这些接口对于类装载器必须可见,否则类装载器将无法链接它们,将会导致类定义失败. 3. 需被代理的所有非 pub ...
- 彻底理解JAVA动态代理
代理设计模式 定义:为其他对象提供一种代理以控制对这个对象的访问. 代理模式的结构如下图所示. 动态代理使用 java动态代理机制以巧妙的方式实现了代理模式的设计理念. 代理模式示例代码 public ...
- 【转】JAVA之动态代理
转自:像少年啦飞驰 代理设计模式 定义:为其他对象提供一种代理以控制对这个对象的访问. 代理模式的结构如下图所示. 动态代理使用 java动态代理机制以巧妙的方式实现了代理模式的设计理念. 代理模式示 ...
- Java设计模式系列之动态代理模式(转载)
代理设计模式 定义:为其他对象提供一种代理以控制对这个对象的访问. 动态代理使用 java动态代理机制以巧妙的方式实现了代理模式的设计理念. 代理模式示例代码 public interface Sub ...
- Java设计模式---(动态)代理模式
代理设计模式 定义:为其他对象提供一种代理以控制对这个对象的访问. 动态代理使用 java动态代理机制以巧妙的方式实现了代理模式的设计理念. 之前虽然会用JDK的动态代理,但是有些问题却一直没有搞明白 ...
随机推荐
- thinkPHP5 引入模板
有三种方法:第一种: 直接使用 return view(); 助手函数第二种: use think\View; class Admin extends View 见下第三种: use think\Co ...
- npm 发包的简易流程
发包的简易流程: https://www.jianshu.com/p/ea64fd01679c 错误集锦: npm publish error: 403. You do not have permi ...
- Asp.Net Core MVC框架内置过滤器
第一部分.MVC框架内置过滤器 下图展示了Asp.Net Core MVC框架默认实现的过滤器的执行顺序: Authorization Filters:身份验证过滤器,处在整个过滤器通道的最顶层.对应 ...
- linux 标准I/O (二)
<Uinx 环境高级编程笔记> 以前经常遇到两种I/O操作 一类是f打头的fopen, fread, fwrite 一类是没有f打头的open, read, fwrite 原来一个是U ...
- 1037 Magic Coupon (25 分)
1037 Magic Coupon (25 分) The magic shop in Mars is offering some magic coupons. Each coupon has an i ...
- make menuconfig时出现 #include CURSES_LOC错误
In :: scripts/kconfig/lxdialog/:: fatal error: curses.h: 没有那个文件或目录 #include CURSES_LOC ^ compilation ...
- [转]Tornado get/post请求异步处理框架分析
转自: http://m.blog.csdn.net/blog/joeyon/41956027 首先说下环境,python2.7,Tornado3.0 然后本文讨论的不是Tornado底层如何用epo ...
- python 9*9示例
# 9*9 乘法表# def nine_nine():# for i in range(1, 10):# for j in range(1, i+1):# ...
- 文件读写’r'和’rb’区别
2012年08月22日 ⁄ 综合 ⁄ 共 849字 ⁄ 字号 小 中 大 ⁄ 评论关闭不管何种语言在进行文件读写时,大家都知道有以下模式: r,rb,w,wb 那么在读写文件时,有无b标识的的主要区别 ...
- 进程池----Pool(老的方式)----回调
之后的进程池使用的是 ProcessPoolExecutor,它的底层使用的就是pool 为什么要有进程池?进程池的概念. 在程序实际处理问题过程中,忙时会有成千上万的任务需要被执行,闲时可能只有零星 ...