views:

2442

answers:

7

I'm new to .Net and I'm trying to understand the basics first. What is the difference between MSIL and Java bytecode?

+2  A: 

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

GHad
Scala can be compiled to target the JVM or the CLR, generating bytecode or MSIL respectively.
Daniel Spiewak
Good to know, but I found another language a month ago or so when reading DZone: Found it! See edit of my post
GHad
+1  A: 

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.

slim
Bytecode is actually very readable as long as you have a hex editor. It's a pretty simple stack-based language with extensions for direct representation of classes and methods. I thought that MSIL was lower-level (e.g. registers)?
Daniel Spiewak
http://en.wikibooks.org/wiki/Transwiki:List_of_hello_world_programs#CILhttp://en.wikibooks.org/wiki/Transwiki:List_of_hello_world_programs#Java_byte-codeOne is the *raw* CIL. The other is *disassembled* byte code.Bytecode may be reasonably readable if you grok hex, but that's not a design aim.
slim
"Disassembled" is really the wrong word for it. "De-encoded" maybe. Bytecode is unreadable in the .class files merely for compactness. Countrary to javap's man page, there's no disassembly involved in producing readable bytecode from a compiled class.
Daniel Spiewak
+1  A: 

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.

Internet Friend
+1  A: 

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.

Jeffrey LeCours
+7  A: 

They are essentially doing the same thing, MSIL is Microsoft's version of Java bytecode.

The main differences internally are:

  1. Bytecode was developed for both compilation and interpretation, while MSIL was developed explicitly for JIT compilation
  2. MSIL was developed to support multiple languages (C# and VB.NET, etc.) versus Bytecode being written for just Java, resulting in Bytecode being more similar to Java syntactically than IL is to any specific .NET language
  3. MSIL has more explicit delineation between value and reference types

A lot more information and a detailed comparison can be found in this article by K John Gough (postscript document)

Guy Starbuck
+19  A: 

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 is 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).

  • .NET allows user defined types that reside on the stack (struct).
  • .NET supports unsigned types, this makes the instruction set a bit richer.
  • Java includes the exception specification of methods in the bytecode. Although exception specification is usually only enforced by the compiler, it may be enforced by the JVM if a class loader other than the default one is used.
  • .NET generics are expressed in IL while Java generics only use type erasure.
  • .NET attributes have no equivalent in Java (is this still true?).
  • .NET enums are not much more than wrappers around integer types while Java enums are pretty much fully fledged classes (thanks to Internet Friend for commenting).
  • .NET has out and ref 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.

Motti
Regarding attributes - Java annotations can be set to appear in the bytecode as well, so there is an equivalent.
Oak
+1  A: 

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

Prints out disassembled code, i.e., the instructions that comprise the Java bytecodes, for each of the methods in the class. These are documented in the Java Virtual Machine Specification.

Both "Java VM instructions" and "MSIL" are assembled into .NET bytecode and Java code, which are not human readable.

Dennis Cheung