Home Topic 4 Last Next


Outline the need for a translation process from a higher level language to machine executable code.

Teaching Note:

For example, compiler, interpreter, virtual machine.



Outline one difference between the functioning of an interpreter and a compiler. [2 marks]

Reminder of 4.3.4  vs.  4.3 5

Assessment statement 4.3.4 was about the need for higher level languages. Why we need, for example, Java.

This assessment statement is about why we need translation processes. (Basically because we need to translate from 0100010001100010100010 to Java.)



The Need for a Translation Process

*** Note at this point that you may not have to know much beyond explaining why the need exists, and, as your answers, simply stating the three examples cited in the Teaching Note. The fact is that there certainly is a need for translation between higher level languages to lower level languages, because we cannot read machine code, and machines cannot directly read languages such as Java.

And what things can we use to effect this translation?

So a simple, straight-forward "answer" to the assessment statement taken at face value can be something like this:

Because of the huge difference between higher (human) and lower (machine) level languages there need to be ways to translate between the two. Compilers, interpreters, and virtual machines are able to do this translation of higher level language code into machine code.



It would not be a bad idea to go into a bit more details about the translators mentioned in the Teaching Note, and it's possible you may even have to have a basic understanding of them.

Interpreted vs Compiled Programs

- An interpreted programming language is one which is translated one line at a time by a translator program on the computer running the program.

- A compiled programming language is converted into machine code all at once by the programmer's computer, and then distributed to the users, so it can be used directly by the machine. Though note that working this way, you have to compile separate versions of your program for the various operating systems it will be used on (an .exe file for Window machines, an .app file for Mac OS and so on).

Because the interpreted program needs to be translated one instruction at a time, it is slower when used. But its one main advantage is because each line can be inspected as run, errors can be caught and handled by the interpreter. This aids debugging during the development process.

In terms of the advantages of compiled programs, not only do they run faster, but they don't require a specific interpreter to be installed on the user's machine.

  Compiled languages Interpreted languages
Description Converted into machine code all at once Translated into machine code one line at a time

- Faster
- No interpreter needed on maching running the code

- One line at a time debugging possible
- So one line at a time error catching possible.

Disadvantages - Harder to debug when coding.
- Harder to catch errors when run.
- Slower

In theory any language can be compiled or interpreted, and many modern languages support both options.

Here is a great YouTube video (from 1983, my High School graduation year!!) about the differences between the way a compiler and an interpreter works.

***** To Dark-grey or not.... With 4.3.5, it's hard to know how much of this is too much....

If it's just "the need", then even the info above about compilers and interpreters is beyond what's needed....*****

Java is Partially Compiled, & then uses a Virtual Machine

Java is actually unique in that it is "partially" compiled, and then compiled further and/or interpreted on the user's machine.

In terms of the particulars, a Java program is first partially compiled by the programmer's IDE into a JAR file - which is a non-machine specific compressed file containing several .class binary files. But then a "Java Virtual Machine" on the user's computer does the final compilation/interpretation of the Java code, for the specific machine on which the program runs. This way one Java JAR file can be run on any operating system which has its machine-specific "Java Virtual Machine" installed, making Java a platform-independent language.

Here is a good YouTube video on how Java works in a platform independent way with the Java Virtual Machine.


**Again, note that all this information goes a bit beyond what is required by this particular assessment statement - though the Java Virtual Machine enters back in in HL Topic 6, as something the OS takes advantage of as a means of simplifying things from the user's standpoint (another example of abstraction). If you want even more information, read on, below. But for now, do go back to the assessment statement itself and recall that the main thing you need to be aware of is the need for compilers and interpreters.



---------------------- EXTRA - FROM FORMER CURRICULUM ----------------------

- 3.1.2 Describe the function of high-level language translators.:

- The translators should be limited to interpreters and compilers.

- Most important is compiling versus Interpreting, during the translation of high level languages into low level machine code.

Most of the JSR Notes in this case are just re-stating what is generally well done in the yellow and red text book:

And though I only infrequently copy and paste from webopedia or wikipedia, in this case, it's hard to state it better. So from Wikepedia.org:

Interpreted language

From Wikipedia, the free encyclopedia
Jump to: navigation, search

Interpreted language is a programming language in which programs are 'indirectly' executed ("interpreted") by an interpreter program. This can be contrasted with a compiled language which is converted into machine code and then 'directly' executed by the host CPU. Theoretically, any language may be compiled or interpreted, so this designation is applied purely because of common implementation practice and not some essential property of a language. Indeed, for some programming languages, there is little performance difference between an interpretive- or compiled-based approach to their implementation.

Many languages have been implemented using both compilers and interpreters, including BASIC, C, Lisp, Pascal, and Python. While Java is translated to a form that is intended to be interpreted, just-in-time compilation is often used to generate machine code.


Note that translation is "parsing", or changing from one form (human words written in a language like Java) to, ultimately - through various steps - the machine code of 0s and 1s specific for a specific kind of computer architecture. The key difference between compiled languages and interpreted languages is that with compiling, the "parsing" is done ahead of time to the whole program all at once, down to the actual machine code which is ready to be executed, whereas with interpretation, the parsing and execution are done by the interpreter when the program is run, one instruction at a time.

Also note that an interpreted program needs an interpreter on any computer that wishes to run it, whereas the compiled file is a complete, totally stand-alone file, like an .exe Windows file, not requiring a specific interpreter.

One interesting thing not mentioned in class is that Java can be both interpreted and compiled. How is this possible? You can refer to the Morelli text, pages 45-49, but basically, Java (along with the even newer C# - pronounced "C-sharp") are not languages that compile all the way to machine code native to a particular kind of computer, as C and C++ do; rather they compile to general byte code, which is then, in turn, is interpreted in a specific way by their respective Java Virtual Machines (VM). So, in this case, you'll note that the Java VM is the "interpreter".

(If I get a chance, I'll do a "Hello World" applet, and insert it here: The fact that I can, and you'll all be able to run it on your computer - no matter what kind of a computer you have - demonstrates this distinct advantage of Java: the ability to do the final interpretation stage in a specific way on each specific computer - assuming that you have the Java Virtual Machine running on your computer, which you almost certainly do.) (I did, but will have to do it again, as it is causing freezing in Firefox.... will do another one some time.)


Linking: This is worth expanding upon a bit. When you compile a Java file, after it's been parsed to "object code" (simplified, compacted code accomplished though the "lexical analysis" stages), it then needs to have all of the Java libraries you've taken advantage of linked to the file. So things like System.out.println, and Math.random need to be included in the overall file.

So for Java, the various stages (all the way from the code you write to the actual executable file on a specific computer) are:

1. Writing the source code - what you write(in NotePad, Netbeans, or whatever)
2A. Compiling your code - when you compile the program in Netbeans or whatever, the compiler checks for syntax correctness, and then makes tokens etc out of the statement you have written.
2B. Linking libraries to your code - in Netbeans, this happens when you run the program; both your lines and the lines of needed external libraries are compiled to an intermediary stage of byte-code. With Java a compressed format .jar file is produced.
3. Interpretation of the byte-code by a specific JVM of a particular computer. (That also happens when you run in Netbeans, but it happens slightly differently on each platform the program runs on.)


JSR To Do: Better yet would be a link to a Java file working through a GlassFish server that will not only allow Java files to be executed on any computer, but to be done so with the capability of storing information on a server, just like Stroodle does with php and a MySQL database. I’m not quite sure how the GlassFish server works, but you can imagine my delight in getting as far as I did in just half an hour or so; this opens up a whole new world of possibilities for what your programs can do: :8080/WebApplication3/ ...Actually, not sure about this one; it's working when I have Netbeans open; it has to "deploy"; I'm a little nervous to keep it deployed until I know a bit more about it - particularly security issues. So I'll show you in class; it's pretty interesting.