Difference between revisions of "Compiling and the Java Virtual Machine"

From CompSciWiki
Jump to: navigation, search
(Redirecting to Java In-depth)
(decided to use this page after all)
Line 1: Line 1:
#REDIRECT [[Java In-depth]]
+
{{1010Topic | 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.|Overview=In this section, you will learn about compilers in general and how the Java compiler is different. 
 +
 
 +
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. |Chapter_TOC=[[Java In-depth]]}}
 +
 
 +
[[Image:Traditional_compiling.PNG|thumb|180px|left|How a traditional program is born.]]
 +
=Compiling=
 +
 
 +
Writing lines of code does nothing by itself.  We have to compile code before we can run it.
 +
 
 +
When you compile a program, a program called the '''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 [[Java Fundamentals|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 [[What Is Programming Part II|further reading]].
 +
 
 +
In general, a compiler takes lines of code and outputs the equivalent machine code that a computer can run.
 +
 
 +
 
 +
 
 +
 
 +
 
 +
 
 +
 
 +
 
 +
 
 +
 
 +
 
 +
 
 +
 
 +
 
 +
 
 +
 
 +
[[Image:Java compiling.PNG|thumb|180px|left|How a Java program is born.]]
 +
=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 understands a different machine language.  With most programming languages, you will need to compile your program differently for each type of computer.  You might even need to change the code itself to run correctly on a certain type of computer!
 +
 
 +
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.  Instead of creating a file that can be run by a computer, the computer runs a [[Java virtual machine]] that understands Java bytecode.  This moves the responsibility of creating programs that work on multiple platforms from a programmer to the creators of Java.
 +
 
 +
Another way of think of what the Java compiler does is by looking 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 example, we are using C++ as a representative traditional programming language, and we are using Windows file extensions.
 +
 
 +
{|
 +
!Programming Language
 +
!Source Code<br />(Pre-compilation)
 +
!Object File<br />(Post-compilation)
 +
!Command to run
 +
  |-
 +
    !C++
 +
    |<code>example.cpp</code>
 +
    |<code>example.exe</code>
 +
    |<code>example</code>
 +
  |-
 +
    !Java
 +
    |<code>example.java</code>
 +
    |<code>example.class</code>
 +
    |<code>java example</code>
 +
|}
 +
 
 +
Notice that when we run our Java program, we are actually running java.exe, the [[Java virtual machine]].

Revision as of 05:37, 6 December 2007

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.

   

{{{Body}}}

How a traditional program is born.

Compiling

Writing lines of code does nothing by itself. We have to compile code before we can run it.

When you compile a program, a program called the 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.

In general, a compiler takes lines of code and outputs the equivalent machine code that a computer can run.









How a Java program is born.

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 understands a different machine language. With most programming languages, you will need to compile your program differently for each type of computer. You might even need to change the code itself to run correctly on a certain type of computer!

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. Instead of creating a file that can be run by a computer, the computer runs a Java virtual machine that understands Java bytecode. This moves the responsibility of creating programs that work on multiple platforms from a programmer to the creators of Java.

Another way of think of what the Java compiler does is by looking 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 example, we are using C++ as a representative 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.