I tried to explain to beginner engineers what bytecode is (as part of explaining the principles of running Java programs). So, I try to explain in a standard way, they say it is something between compilation and interpretation, such as each command of Java code byte, it’s like several assembler commands, but they look at me and say - it’s not clear! They are not programmers, they do not know what compilation is, neither interpretation, nor assembler, I tried to explain them with my fingers, and something seemed to come to them, but how can I use life examples on my fingers to explain the principle of bytecode operation?

  • 14
    If they are not programmers, then they do not need it. Do not waste time. - nitrocaster
  • five
  • I myself am not a programmer (by training), now there is the question of calculating the cooling of the combustion chamber of a rocket engine. So I want to give three students this task, in three different languages, and see who is better to succeed. Languages: java-environment eclipse, C ++ - Visual Studio and Visual Basic, and even 6. I myself put it in MathCAD and MathLab. I think it should be a good experiment! - vanyamelikov
  • one
    Sorry for the bluntness - but it feels like a touch, as if the respondents themselves do not fully understand, except for @KoVadim, what is byte code. No need to minus a minus! Ponder the question was that it was necessary to explain the byte code, subject to understanding a little concepts of compilation and interpretation! And many definitions can be attributed to both the first and the second and third! - albertmalbert
  • I wanted to write, but I looked into Wikipedia and realized that I could not add anything. @vanyamelikov, and your colleagues read this article? - avp

7 answers 7

And you need to explain very simply. Using their position. To begin with, we show these engineers a Java program. For example, the classic HelloWorld. And we ask - is it clear? Most likely they will say no. We explain that the special program that executes the code is also not clear. To do this, you need to "disassemble the bones."

Now we do "pseudo-branching" - as for a typist (secretary). For HelloWorld, it will be like this.

  • set up the environment (in the code it is not, but it is automatic) - prepare the paper, check the cartridges.
  • take the string "hello world" from memory.
  • Press the buttons, enter the date by letter (here appeared a cycle :)).
  • clean everything up and take the paper to the customer.

Formally, this is simple bytecode. Only this is human bytecode. And if in it to standardize all operations and to number, then everything can be reduced to a set of numbers. Now it’s easy to go to the real bytecode.

It will be easy to explain and portability. If a person (secretary) learns all the operation codes, then she will be able to do any work, the main thing is to have a sequence of codes. And engineers can try to solder-construct a device that will execute it.

  • one
    Great answer! Especially the typist example!) - vanyamelikov
  • 2
    aha, usually it is called a pseudo-code, it is easier to explain at the program-example, heWord, but if the secretary has 4 size, it can be simplified. - Gorets
  • one
    Why? For specific platforms where it is executed on the processor itself, it will be quite machine code. True, for other platforms it will also be byte-code. - gecube
  • 3
    Byte code or machine code - depends only on your attitude to it. If you look deeper, even what many people call "real assembler and true machine code" (at the moment I'm talking about Intel x86 and 64 bit processors, I can not vouch for others) is also bytecode, as some instructions The processor inside is implemented as a microcode. A modern processor in a sense of the word - jit compiler - all these predictions of transitions, permutation of instructions. - KoVadim
  • one
    @KoVadim, I agree - the processor is a complex thing and has its own microcode. After all, it is known that modern processors have a CISC interface outside and a RISC core inside themselves. So it is possible to build more high-performance chips using heaps of cool technologies (reordering of commands, pairing commands, renaming registers, etc.) than if you would do everything in the old fashioned way. - gecube

@vanyamelikov in my opinion you yourself do not understand what Java bytecode is :)

Java bytecode is machine instructions for a non-existent Java machine, often called the Java Virtual Machine.

What is the strength in, brother? And the power of a brother is that you write for a certain Java machine (someday someone will do a physical Java machine). What is the portability of Java code: it's just a matter of implementing a Java machine on some real machine, and so on.

  • one
    Initially, the creators of the bytecode designed it as a system of commands and hoped that the corresponding processors would be created. And indeed, they were quickly created, but lost for economic reasons. It turned out to be cheaper to use traditional processors using bytecode recompiling. - rfq
  • Initially - when is this? Bytecode and similar solutions appeared to him long before java. - alexlz
  • @rfq can link to the physical processor directly understand Java bytecodes? - Barmaley
  • @alexz was meant by the "creators of java-bytecode" @Barmaley google.com/search?q=java+bycode+processor - rfq

I would try to explain something like this:

Computers "think" with the simplest instructions, and when you write a program, say, C, it is converted into a set of primitive machine instructions. For example, there was an expression in the programming language

a = 42 * spam 

It is roughly translated into a sequence of instructions.

 достать значение из spam умножить его с 42 положить результат в a 

Since computers are different, the instructions that they use are also not always the same.

Therefore, they invented a layer on these very machine instructions - byte code. Your code written in languages ​​such as java and python is converted first to it and then to machine instructions. Why do you need it? The same byte code will work the same on different computers. Roughly speaking, a byte code is a “wrapper” over machine instructions, which increases the portability of programs.

I will be glad to hear criticism about this explanation ^ _ ^

  • Thanks a great answer! Many give answers for which baytkod is no different from compilation and interpretation, the feeling that many do not feel different, and they send me to Wikipedia, as if only idiots around who did not know about its existence, wanted to hear some original definition, but in the end I heard a few rehash of the standard! Do not judge strictly, but really the impression! - vanyamelikov
  • @vanyamelikov Please, glad that you liked ^ _ ^. Of course there is something like this in Wikipedia, but everything is written there too difficult (and therefore it’s problematic to understand new things for it). I'm not sure that I managed to explain everything without errors. In general, it is quite difficult to explain things so that it is simple and clear. - gg4me
  • one
    @venyamelikov, you just need to carefully read Wikipedia: The program on the byte-code is usually executed by the byte-code interpreter (usually it is called a virtual machine, because it is similar to a computer) ..... The byte-code is called that because each operation code is one bytes, but the length of the command code is different. All the rest is the rephrasing of precisely these phrases and sucking the consequences (possibly for someone unobvious) of this fact. - avp

As I understand it, the question is how to explain what byte-code is without resorting to explaining the rest of the context. It seems to me that it will not be possible to explain what a byte code is without explaining what a machine language and a programming language are, because it’s not in itself, it’s a layer between them - that is, in order to understand what the interlayer does, you need to understand what it lies between.

  • I say a lot of things they are already beginning to understand, they even moved from Hello World, they are not hopeless. The problem is that they do not understand the difference. They say - and why byte code, when there is a compilation (which they hopefully understood!) - vanyamelikov
  • > The problem is that they do not understand the difference. Do students have internet? Wikipedia did not ban them? If they are not hopeless, they are able to find the articles indicated in the comments to your question. - nitrocaster

The best practical way:

  1. show the basics of programming (primitive types, arrays, loops, input / output)
  2. Let everyone write an emulator of a certain (virtual) computer: each student has his own system of commands, you can give a very brief overview of the architecture classes: a stack machine, RISC with 8-16 registers, CISC a la Z80 (register-battery commands and memory-battery ), 3-address machine of type memory-memory-memory; Harvard / Fonneiman (1 or 2 arrays for code / data). At the same time, you will show the concepts of machine code, code / data coding, memory organization, input / output - this is also included in the basic training program for technical skills. And all this is clear, you can fasten the GUI, and even in the minimum console version, the student can add a log of execution of any command, or memory changes.
  3. "But the машинный код your виртуальной ЭВМ , which is stored in your PROM array, is the byte code: all its difference from machine code is that it is executed not by a real processor, but by your interpreter program. Your interpreter program is a виртуальная машина . If you are interested, in the book [DragonBook] you can find algorithms and methods how to convert your bytecode to real machine code and execute it on a real processor - this is done by the JIT-компилятор Java JIT-компилятор . "
  4. The byte code is also an промежуточным представлением программы [DragonBook] - between the program on the HLL and the machine code. Since the development of the Java language, it was assumed that Java programs should be executed on any computer with an unknown type of processor, instead of compiling (into machine code), the format for distributing programs as bytecode files was chosen. The format of this bytecode, command system, and memory organization are standardized, and are described in the виртуальной машины JVM specification. реализации JVM were also written for common computers, for example, for PCs, and mobile phones are programs that do the same thing you did: interpret bytecode, and some implementations can compile it into RAM directly during the execution of bytecode ( JIT), which greatly increases the speed of program execution.
  5. It was also developed several Java-processors that perform bytecode hardware, that is, for them it is machine code. But they didn’t get widespread, as it turned out to be cheaper to use widespread cheap processors, a JIT compiler and 100,500 ready-made libraries (including operating systems) written in C and Fortran.
  6. And now we take clause 2, set ANTLR, and write the compiler of a simple programming language (and show how to parse text data - the necessary thing for engineers, you can write an assembler as coursework)

    Baytkod is a system of commands of a certain processor. Do they know that every processor has a command system? Have you seen an x86 assembler program? If not, show me. Then, the program in any command system can be executed by hardware, software (interpreter), and can be transferred to another command system, and then executed hardware, software, or ... It so happened that the java-bytecode is interpreted or translated, and x86 is executed hardware. But the reasons are economic, not fundamental. Java processors exist, but are not widely used. On other planets, maybe the opposite is true.

      Why invented byte code? In the beginning, there were interpreters that simply executed the code from the text, but since the efficiency in constant parsing of lines is not good, they came up with working with bytes, and not as with the source code of lines, that is, the program converts the initial syntax of the PL into a byte code where there are instructions and tags, etc. almost as in ASM, hence the name of the virtual machine, and not the interpreter, for example in Java it is the JVM. So everything is simple, the virtual machine, when loading bytecode, executes it. But this is not all, for greater performance JIT was introduced, that is, bytecode compilation into native code in the course of execution, thus not very much we get competitive programs for compiled YaPs.