Compiling and the Java Virtual Machine

From CompSciWiki
Jump to: navigation, search

COMP 1010 Home > Java In-depth


Introduction

Before you can run your Java program, you must compile it. Java is different than most programming languages because a program compiled on your computer will work on any other device that has a Java Virtual Machine, such as a cell phone.

   

You do not need to understand everything that is covered in this section. The point of this section is to give you a basic understanding of how lines of code becomes a program that your computer can run, and why Java does this differently.

How a traditional program is born.
How a Java program is born.

Compiling

Writing lines of code does nothing by itself. To make our lines of code actually do something, we have to start speaking in a language the computer will understand completely. We call this translation process compiling.

When you compile code, a program called a compiler translates text written in one programming language into another programming language. Java is a programming language that is (relatively) comprehensible by humans; it takes a while to learn, but once you learn the fundamentals of Java, you can read another Java program and understand what it is doing. A computer, on the other hand, understands a different language that is not comprehensible by humans; each instruction is encoded as a sequence of 0's and 1's (e.g., 4+5 might be 1010 1000 1011 0100). The language that a computer understands is called machine code, or machine language. Machine code is explained in greater detail in Chapter 2's further reading (note that the further reading is not examinable material).

To put it simply, a compiler converts code we understand to code that computers understand.

The Java Compiler

One of the reasons Java is different than other languages is that it is designed to be platform independent.

We use many different types of computers every day: Windows PCs, Macs, cell phones, game consoles, etc. Each of these computers is different, and each understands a different machine language. With most programming languages, you would need to compile your program differently for each type of computer. You might even need to change the code itself to run correctly on certain types of computers.

Java is different. It is designed to be run on any type of computer. To do this, the Java compiler does not convert lines of code to machine code, it converts lines of code to another language called Java bytecode. You can think of Java bytecode as a happy medium between Java code and machine code. Since computers do not understand Java bytecode, they run a Java virtual machine that translates Java bytecode to machine code on-the-fly (while the program is running). This moves the responsibility of creating programs that work on multiple platforms from the programmer to the creators of Java.

Another way of think about what the Java compiler does is to look at file types. You are probably aware of most common file types, like .doc for Microsoft Word documents, .mp3 for MP3 encoded music files, and so on. The table below details the file types that are involved when programming in a traditional programming language and the Java programming language. In the table, we are using C++ (a programming language that you can learn about in second year computer science courses) as our traditional programming language, and we are using Windows file extensions.

Programming Language Source Code
(Pre-compilation)
Object File
(Post-compilation)
Command to run
C++ example.cpp example.exe example
Java example.java example.class java example

Notice that when we run our Java program, we are actually running java.exe, the Java virtual machine.

The Java Virtual Machine

The Java virtual machine (JVM) is a piece of software that runs Java programs. More specifically, the JVM runs the Java bytecode generated by the Java compiler.

The concept of a virtual machine is a bit strange: instead of talking directly with the hardware of the computer, Java programs talk to the JVM, which talks to the hardware of the computer. At first, this seems like an added layer of complexity, but in actuality, the JVM simplifies programming. For most programming languages, if we write a program for Windows PCs, that program will only run on Windows PCs. If we write a program in Java, that program will run on any computer that has a JVM. Now we only have to worry about one platform instead of many. JVMs exist for many platforms, including most operating systems (Windows, Mac OS X, Linux, etc.), handheld devices like palm pilots, cell phones, and so on. The programs that you are writing can run on all of these devices!

Platform independence makes Java very attractive to people and companies that want to create software for many platforms. However, simplicity comes at a price. A program running through the JVM will be slightly slower than an equivalent program running directly. Many platforms offer us useful tools; the JVM cannot take advantage of many of these tools. Most platforms have their own way of interacting with the user - often called a platform's "look-and-feel." Since Java programs should look very similar on all platforms, they can stick out like a sore thumb compared to other applications. Finally, in the event that there is a serious bug in a JVM, every Java program will be affected. In this situation, there is little a programmer can do except use an old version of the JVM until the problem is resolved.

Learning a programming language's advantages and disadvantages will help you when you get to choose the language you will use for a project. Java is useful in many situations, due in large part to the JVM.