MSIL 和 Java 字节码之间的区别?
我是 .Net 的新手,我想首先了解基础知识。 MSIL 和 Java 字节码有什么区别?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我是 .Net 的新手,我想首先了解基础知识。 MSIL 和 Java 字节码有什么区别?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(8)
CIL 又名 MSIL 旨在人类可读。 Java 字节码不是。
将 Java 字节码视为不存在的硬件(但由 JVM 模拟)的机器代码。
CIL 更像是汇编语言——距离机器代码一步之遥,同时仍然是人类可读的。
CIL aka MSIL is intended to be human-readable. Java bytecode is not.
Think of Java bytecode as being machine code for hardware that does not exist (but which JVMs emulate).
CIL is more like assembly language - one step from machine code, while still being human-readable.
Serge Lidin 撰写了一本关于 MSIL 详细信息的不错的书:Expert .NET 2.0 IL Assembler。 我还能够通过使用 .NET Reflector 和 Ildasm(教程)。
MSIL 和 Java 字节码之间的概念非常相似。
Serge Lidin authored a decent book on the details of MSIL: Expert .NET 2.0 IL Assembler. I also was able to pick up MSIL quickly by looking at simple methods using .NET Reflector and Ildasm (Tutorial).
The concepts between MSIL and Java bytecode are very similar.
没有那么多差异。 两者都是您编写的代码的中间格式。 执行时,虚拟机将执行托管的中间语言,这意味着虚拟机控制变量和调用。 甚至有一种我现在不记得的语言可以以同样的方式在 .Net 和 Java 上运行。
基本上,它只是同一事物的另一种格式
编辑:找到语言(除了 Scala):它是 FAN(http:// www.fandev.org/),看起来很有趣,但还没时间评估
There are not that much differences. Both are intermediate formats of the code you wrote. When executed, the Virtual machines will execute the intermediate language managed that means that the Virtual Machine controls the variables and calls. There is even a language which I don't remeber right now that can run at .Net and Java the same way.
Basicly, it's just another format for the same thing
Edit: Found the language (besides Scala): It's FAN (http://www.fandev.org/), looks very interesting, but no time yet to evaluate
同意,差异很小,作为初学者可以忽略。 如果您想从基础开始学习 .Net,我建议您查看公共语言基础结构和公共类型系统。
Agreed, the differences are minute enough to ingore as a beginner. If you want to learn .Net starting from the basics, I'd recommend looking at the Common Language Infrastructure, and the Common Type System.
我认为MSIL不应该与Java字节码相比较,而是“组成Java字节码的指令”。
没有反汇编的java字节码的名称。 “Java Bytecode”应该是一个非官方的别名,因为我在官方文档中找不到它的名字。
Java 类文件反汇编器 说
“Java VM指令”和“MSIL”都被组装成.NET字节码和Java代码,这些都是人类无法阅读的。
I think MSIL should not compare to Java bytecode, but "the instruction that comprise the Java bytecodes".
There is no name of disassembled java bytecode. "Java Bytecode" should be an unofficial alias, as I cannot find its name in official document.
The Java Class File Disassembler say
Both "Java VM instructions" and "MSIL" are assembled into .NET bytecode and Java code, which are not human readable.
它们本质上在做同样的事情,MSIL 是 Microsoft 版本的 Java 字节码。
内部的主要区别是:
更多信息和详细比较可以在 K John Gough 的这篇文章(后记文档)
They are essentially doing the same thing, MSIL is Microsoft's version of Java bytecode.
The main differences internally are:
A lot more information and a detailed comparison can be found in this article by K John Gough (postscript document)
首先我要说的是,我不认为 Java 字节码和 MSIL 之间的细微差别会困扰 .NET 开发新手。 它们都具有相同的目的,即定义一个抽象目标机器,该目标机器是最终使用的物理机器之上的一层。
MSIL 和 Java 字节码非常相似,实际上有一个名为 Grasshopper 它将 MSIL 转换为 Java 字节码,我是 Grasshopper 开发团队的一员,所以我可以分享一些我的(褪色的)知识。
请注意,当 .NET Framework 2.0 发布时,我就停止了这方面的工作,因此其中一些内容可能不再正确(如果是这样,请发表评论,我会更正它)。
struct
) 不同的值语义。enum
只不过是整数类型的包装器,而 Javaenums
几乎是完全成熟的类(感谢 网友评论)。out
和ref
参数。还有其他语言差异,但大多数都不是在字节码级别上表达的,例如,如果没记错的话,Java 的非静态内部类(.NET 中不存在)不是一个字节码功能时,编译器会为内部类的构造函数生成一个附加参数并传递外部对象。 .NET lambda 表达式也是如此。
First off let me say that I don't think that the subtle differences between Java bytecode and MSIL is something that should bother a novice .NET developer. They both serve the same purpose of defining an abstract target machine which is a layer above the physical machine being used in the end.
MSIL and Java bytecode are very similar, in fact there
iswas a tool called Grasshopper which translates MSIL to Java bytecode, I was part of the development team for Grasshopper so I can share a bit of my (faded) knowledge.Please note that I stopped working on this around when .NET framework 2.0 came out so some of these things may not be true any more (if so please leave a comment and I'll correct it).
struct
).enums
are not much more than wrappers around integer types while Javaenums
are pretty much fully fledged classes (thanks to Internet Friend for commenting).out
andref
parameters.There are other language differences but most of them are not expressed at the byte code level, for example, if memory serves, Java's non-
static
inner classes (which do not exist in .NET) are not a bytecode feature, the compiler generates an additional argument to the inner class's constructor and passes the outer object. The same is true for .NET lambda expressions.CIL(MSIL 的专有名称)和 Java 字节码的相同点多于不同点。 但存在一些重要的区别:
1) CIL 从一开始就被设计为作为多种语言的目标。 因此,它支持更丰富的类型系统,包括有符号和无符号类型、值类型、指针、属性、委托、事件、泛型、具有单个根的对象系统等等。 CIL 支持初始 CLR 语言(C# 和 VB.NET)不需要的功能,例如全局函数和尾调用优化 。 相比之下,Java 字节码被设计为 Java 语言的目标,并反映了 Java 本身的许多限制。 使用Java字节码编写C或Scheme会困难得多。
2) CIL 旨在轻松集成到本机库和非托管代码中
3) Java 字节码设计为可解释或编译,而 CIL 的设计仅假设 JIT 编译。 也就是说,Mono 的初始实现使用了解释器而不是 JIT。
4) CIL 被设计(并指定)为人类可读可写的汇编语言直接映射到字节码形式的形式。 我相信 Java 字节码(顾名思义)只能是机器可读的。 当然,Java字节码相对容易反编译回原始Java,并且如下所示,它也可以被“反汇编”。
我应该指出,JVM(其中大多数)比 CLR(其中任何一个)优化程度更高。 因此,原始性能可能是首选针对 Java 字节码的一个原因。 但这是一个实现细节。
有人说 Java 字节码是为多平台设计的,而 CIL 是为 Windows 设计的。 不是这种情况。 .NET 框架中有一些“Windows”主义,但 CIL 中没有。
作为上面第 4) 点的示例,我不久前编写了一个 Java 到 CIL 编译器的玩具。 如果您向该编译器提供以下 Java 程序:
我的编译器将输出以下 CIL:
这是一个有效的 CIL 程序,可以将其输入到像 ilasm.exe 这样的 CIL 汇编器中以创建可执行文件。 正如您所看到的,CIL 是一种完全人类可读可写的语言。 您可以在任何文本编辑器中轻松创建有效的 CIL 程序。
您还可以使用
javac
编译器编译上面的 Java 程序,然后通过javap
“反汇编器”运行生成的类文件,得到以下内容:javap< /code> 输出不可编译(据我所知),但如果将其与上面的 CIL 输出进行比较,您会发现两者非常相似。
CIL (the proper name for MSIL) and Java bytecode are more the same than they are different. There are some important differences though:
1) CIL was designed from the beginning to serve as a target for multiple languages. As such, it supports a much richer type system including signed and unsigned types, value types, pointers, properties, delegates, events, generics, an object-system with a single root, and more. CIL supports features not required for the initial CLR languages (C# and VB.NET) such as global functions and tail-call optimizations. In comparision, Java bytecode was designed as a target for the Java language and reflects many of the constraints found in Java itself. It would be a lot harder to write C or Scheme using Java bytecode.
2) CIL was designed to integrate easily into native libraries and unmanaged code
3) Java bytecode was designed to be either interpreted or compiled while CIL was designed assuming JIT compilation only. That said, the initial implementation of Mono used an interpreter instead of a JIT.
4) CIL was designed (and specified) to have a human readable and writable assembly language form that maps directly to the bytecode form. I believe that Java bytecode was (as the name implies) meant to be only machine readable. Of course, Java bytecode is relatively easily decompiled back to the original Java and, as shown below, it can also be "disassembled".
I should note that the JVM (most of them) is more highly optimized than the CLR (any of them). So, raw performance might be a reason to prefer targeting Java bytecode. This is an implementation detail though.
Some people say that the Java bytecode was designed to be multi-platform while CIL was designed to be Windows only. This is not the case. There are some "Windows"isms in the .NET framework but there are none in CIL.
As an example of point number 4) above, I wrote a toy Java to CIL compiler a while back. If you feed this compiler the following Java program:
my compiler will spit out the following CIL:
This is a valid CIL program that can be fed into a CIL assembler like
ilasm.exe
to create an executable. As you can see, CIL is a fully human readable and writable language. You can easily create valid CIL programs in any text editor.You can also compile the Java program above with the
javac
compiler and then run the resulting class files through thejavap
"disassembler" to get the following:The
javap
output is not compilable (to my knowledge) but if you compare it to the CIL output above you can see that the two are very similar.