不同的Java编译器(供应商不同)会产生不同的字节码 [英] Do different Java Compilers (where the vendor is different) produce different bytecode

查看:197
本文介绍了不同的Java编译器(供应商不同)会产生不同的字节码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于相同的主要版本,比如Java 7,做不同的Java编译器(例如,Oracle的热点,JRockit或IBM的J9等......)将给定的java源代码文件编译成相同的bytcode?

扫描 Java 7语言规范似乎正在讨论的是语言的语义,而不是代码转换为字节码。

Scanning the Java 7 language spec it would seem that what is being discussed is the semantics of the language and not the transformation of the code into bytecode.

这个问题与不同的major.minor版本不同,给定供应商生成相同的字节码。 此处已经回答了这个问题 - 用可以

This question is not the same as do different major.minor versions for a given vendor produce the same bytecode. That question is already answered here - with a could be.

来自以下回答 Java类文件的创建是否具有确定性?以及对这个的答案的评论以及上面的major.minor问题的两个答案1 2 ,我收集到我的问题的答案是

From the following answer to Is the creation of Java class files deterministic? and a comment to that answer that refers to this along side two answers to the major.minor question above 1 and 2, I gather that the answer to my question is YES.

上述摘录如下:


JLS使许多实现细节从一个
实现变为另一个。

The JLS leaves many implementation details to vary from one implementation to another.


然而,JLS没有指定从源代码到1的1:1映射
生成字节代码,因此您不应该依赖于生成的完全相同的字节
代码。

However the JLS does not specify a 1:1 mapping from source code to the generated byte code, so you should not rely on the exact same byte code to be generated.

然而此处的评论意味着不同:

Yet a comment here implies differently:


这是编译器,即javac,使用
BLAH BLAH BLAH创建代码。这与HotSpot无关。

It’s the compiler, i.e. javac, creating the code using a BLAH BLAH BLAH. This has nothing to do with HotSpot.

这意味着给定代码X所有javac实现(相同版本/不同供应商)必须生成相同的Y字节码。

It implies that given a code X all javac implementations (same version/different vendors) must produce the same Y bytecode.

我看不出那是怎么回事,但我无法证实它不是(或者我认为,见上文)是正确的。

I cannot see how that is so, but I am unable to verify that it is not (or that what I think, see above) is correct.

可以给出明确的答案吗?

Can a definitive answer be given?

推荐答案

编译器之间的差异以及有趣的一些允许的差异导致了过去的问题。

There are differences between the compilers and interestingly some of the permitted differences led to problems in the past.

一些差异很小,例如一些编译器优化 x = x + 1 以生成与 x ++ 相同的字节码,其他编译器则没有。

Some differences are small, e.g. some compilers optimize x=x+1 to produce the same bytecode as x++, others don’t.

其他人可能会产生更大的影响,例如标准没有指定如何生成过去用于实现私有成员(和类似事物)的内部类访问的合成成员的名称(我不知道它是否在今天)。但计算默认 <$的算法c $ c> serialVersionUID 对所有类成员使用哈希码,甚至是合成成员。

Others can have more impact, e.g. the standard did not specify how to generate the names of synthetic members used to implement inner class access to private members (and similar things) in the past (I don’t know whether it does today). But the algorithm for calculating a default serialVersionUID used a hash code over all class members, even synthetic ones.

因此,编译时 javac 或第一个Eclipse版本创建了具有不兼容的 serialVersionUID 的类。今天,Eclipse对合成成员使用与 javac 相同的名称模式,并发出关于在<中缺少显式 serialVersionUID 的警告code> Serializable 默认情况下的类。

As a consequence, compiling with javac or the first Eclipse versions created classes with incompatible serialVersionUIDs. Today, Eclipse uses the same name schema for synthetic members as javac and issues a warning about missing explicit serialVersionUIDs in Serializable classes by default.

还有很多自由,甚至包装 pack200 和解包可能会创建具有与原始类不同的字节代码的类。

There’s still a lot of freedom and even packing with pack200 and unpacking may create classes with different byte code than the original classes.

这篇关于不同的Java编译器(供应商不同)会产生不同的字节码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆