<p>It seems like anything you can do with bytecode you can do just as easily and much faster in native code. In theory, you could even retain platform and language independence by distributing programs and libraries in bytecode then compiling to native code at installation, rather than JITing it.</p>

<p>So in general, when would you want to execute bytecode instead of native?</p>

[{'answer_id': 48147, 'author': 'Kyle Cronin', 'author_id': 658, 'author_profile': 'https://Stackoverflow.com/users/658', 'pm_score': 2, 'selected': False, 'text': '<p>I think you just answered your own question: platform independence. Platform-independent bytecode is produced and distributed to its target platform. When executed it\'s quickly compiled to native code either before execution begins, or simultaneously (<a href="http://en.wikipedia.org/wiki/Just-in-time_compilation" rel="nofollow noreferrer">Just In Time</a>). The Java JVM and presumably the .NET runtimes operate on this principle.</p>\n'}, {'answer_id': 48151, 'author': 'Brian R. Bondy', 'author_id': 3153, 'author_profile': 'https://Stackoverflow.com/users/3153', 'pm_score': 3, 'selected': False, 'text': '<p>Bytecode creates an extra level of indirection.</p>\n\n<p>The advantages of this extra level of indirection are:</p>\n\n<ul>\n<li>Platform independence</li>\n<li>Can create any number of programming languages (syntax) and have them compile down to the same bytecode.</li>\n<li>Could easily create cross language converters</li>\n<li>x86, x64, and IA64 no longer need to be compiled as seperate binaries. Only the proper virtual machine needs to be installed.</li>\n<li>Each OS simply needs to create a virtual machine and it will have support for the same program.</li>\n<li>Just in time compilation allows you to update a program just by replacing a single patched source file.  (Very beneficial for web pages)</li>\n</ul>\n\n<p>Some of the disadvantages:</p>\n\n<ul>\n<li>Performance</li>\n<li>Easier to decompile</li>\n</ul>\n'}, {'answer_id': 48153, 'author': 'Sean', 'author_id': 4919, 'author_profile': 'https://Stackoverflow.com/users/4919', 'pm_score': 6, 'selected': True, 'text': '<p>Hank Shiffman from SGI said (a long time ago, but it\'s till true):</p>\n\n<blockquote>\n  <p>There are three advantages of Java\n  using byte code instead of going to\n  the native code of the system:</p>\n  \n  <ol>\n  <li><p><strong>Portability</strong>: Each kind of computer has its unique instruction\n  set. While some processors include the\n  instructions for their predecessors,\n  it\'s generally true that a program\n  that runs on one kind of computer\n  won\'t run on any other. Add in the\n  services provided by the operating\n  system, which each system describes in\n  its own unique way, and you have a\n  compatibility problem. In general, you\n  can\'t write and compile a program for\n  one kind of system and run it on any\n  other without a lot of work. Java gets\n  around this limitation by inserting\n  its virtual machine between the\n  application and the real environment\n  (computer + operating system). If an\n  application is compiled to Java byte\n  code and that byte code is interpreted\n  the same way in every environment then\n  you can write a single program which\n  will work on all the different\n  platforms where Java is supported.\n  (That\'s the theory, anyway. In\n  practice there are always small\n  incompatibilities lying in wait for\n  the programmer.)</p></li>\n  <li><p><strong>Security</strong>: One of Java\'s virtues is its integration into the Web. Load\n  a web page that uses Java into your\n  browser and the Java code is\n  automatically downloaded and executed.\n  But what if the code destroys files,\n  whether through malice or sloppiness\n  on the programmer\'s part? Java\n  prevents downloaded applets from doing\n  anything destructive by disallowing\n  potentially dangerous operations.\n  Before it allows the code to run it\n  examines it for attempts to bypass\n  security. It verifies that data is\n  used consistently: code that\n  manipulates a data item as an integer\n  at one stage and then tries to use it\n  as a pointer later will be caught and\n  prevented from executing. (The Java\n  language doesn\'t allow pointer\n  arithmetic, so you can\'t write Java\n  code to do what we just described.\n  However, there is nothing to prevent\n  someone from writing destructive byte\n  code themselves using a hexadecimal\n  editor or even building a Java byte\n  code assembler.) It generally isn\'t\n  possible to analyze a program\'s\n  machine code before execution and\n  determine whether it does anything\n  bad. Tricks like writing\n  self-modifying code mean that the evil\n  operations may not even exist until\n  later. But Java byte code was designed\n  for this kind of validation: it\n  doesn\'t have the instructions a\n  malicious programmer would use to hide\n  their assault.</p></li>\n  <li><p><strong>Size</strong>: In the microprocessor world RISC is generally preferable\n  over CISC. It\'s better to have a small\n  instruction set and use many fast\n  instructions to do a job than to have\n  many complex operations implemented as\n  single instructions. RISC designs\n  require fewer gates on the chip to\n  implement their instructions, allowing\n  for more room for pipelines and other\n  techniques to make each instruction\n  faster. In an interpreter, however,\n  none of this matters. If you want to\n  implement a single instruction for the\n  switch statement with a variable\n  length depending on the number of case\n  clauses, there\'s no reason not to do\n  so. In fact, a complex instruction set\n  is an advantage for a web-based\n  language: it means that the same\n  program will be smaller (fewer\n  instructions of greater complexity),\n  which means less time to transfer\n  across our speed-limited network.</p></li>\n  </ol>\n</blockquote>\n\n<p>So when considering byte code vs native, consider which trade-offs you want to make between portability, security, size, and execution speed. If speed is the only important factor, go native. If any of the others are more important, go with bytecode.</p>\n\n<p>I\'ll also add that maintaining a series of OS and architecture-targeted compilations of the same code base for every release can become very tedious. It\'s a huge win to use the same Java bytecode on multiple platforms and have it "just work."</p>\n'}, {'answer_id': 48156, 'author': 'Josh Segall', 'author_id': 2659, 'author_profile': 'https://Stackoverflow.com/users/2659', 'pm_score': 1, 'selected': False, 'text': "<p>Ideally you would have portable bytecode that compiles Just In Time to native code.  I think the reason bytecode interpreters exist without JIT is due primarily to the practical fact that native code compilation adds complexity to a virtual machine.  It takes time to build, debug, and maintain that additional component.  Not everyone has the time or resources to make that commitment.</p>\n\n<p>A secondary factor is safety.  It's much easier to verify an interpreter won't crash than to guarantee the same for native code.</p>\n\n<p>Third is performance.  It can often take more time to generate machine code than to interpret bytecode for small pieces of code that only run once.</p>\n"}, {'answer_id': 48159, 'author': 'DGentry', 'author_id': 4761, 'author_profile': 'https://Stackoverflow.com/users/4761', 'pm_score': 4, 'selected': False, 'text': '<p>The performance of essentially any program will improve if it is compiled, executed with profiling, and the results fed back into the compiler for a second pass. The code paths which are actually used will be more aggressively optimized, loops unrolled to exactly the right degree, and the hot instruction paths arranged to maximize I$ hits.</p>\n\n<p>All good stuff, yet it is almost never done because it is annoying to go through so many steps to build a binary.</p>\n\n<p>This is the advantage of running the bytecode for a while before compiling it to native code: profiling information is automatically available. The result after Just-In-Time compilation is highly optimized native code for the specific data the program is processing.</p>\n\n<p>Being able to run the bytecode also enables more aggressive native optimization than a static compiler could safely use. For example if one of the arguments to a function is noted to always be NULL, all handling for that argument can simply be omitted from the native code. There will be a brief validity check of the arguments in the function prologue, if that argument is not NULL the VM aborts back to the bytecode and starts profiling again.</p>\n'}, {'answer_id': 48160, 'author': 'Marcio Aguiar', 'author_id': 4213, 'author_profile': 'https://Stackoverflow.com/users/4213', 'pm_score': 2, 'selected': False, 'text': '<p>Here: <a href="http://slashdot.org/developers/02/01/31/013247.shtml" rel="nofollow noreferrer">http://slashdot.org/developers/02/01/31/013247.shtml</a></p>\n\n<p>Go see what the geeks of Slashdot have to say about it! Little dated, but very good comments!</p>\n'}, {'answer_id': 418221, 'author': 'Mike Dunlavey', 'author_id': 23771, 'author_profile': 'https://Stackoverflow.com/users/23771', 'pm_score': 2, 'selected': False, 'text': '<p>All good answers, but my hot-button has been hit - performance.</p>\n\n<p>If the code being run spends all its time calling library/system routines - file operations, database operations, sending windows messages, then it doesn\'t matter very much if it\'s JITted, because most of the clock time is spent waiting for those lower-level operations to complete.</p>\n\n<p>However, <strong>if</strong> the code contains things we usually call "algorithms", that have to be fast and don\'t spend much time calling functions, <strong>and</strong> if those are used often enough to be a performance problem, then JIT is very important.</p>\n'}, {'answer_id': 16200636, 'author': 'Fiery Phoenix', 'author_id': 2128915, 'author_profile': 'https://Stackoverflow.com/users/2128915', 'pm_score': 0, 'selected': False, 'text': '<p>Portability and platform independence are probably the most notable advantages of bytecode over native code. </p>\n'}]
['https://Stackoverflow.com/questions/48144', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1891/']