200 likes | 283 Views
Using the Java programming language compiler. Review of relevant material from previous lectures. From previous lectures:. A computer can only execute machine instruction codes (binary numbers) Writing algorithms in machine instruction codes is extremely tedious.
E N D
Review of relevant material from previous lectures • From previous lectures: • A computer can only execute machine instruction codes (binary numbers) • Writing algorithms in machine instruction codes is extremely tedious
Review of relevant material from previous lectures (cont.) • People have invented a number of English-like languages that is highly suitable for writing algorithms • People have also written computer applications that translate algorithm written in a programming language into machine instructions • These languages are called (high level) programming languages • These applications are called compilers
How to write an algorithm that a computer can execute • The hard way: You will hate your professor if he/she make you do this.... (and yet, the pioneers in Computer Science had done this... we must be grateful to them) • Write the algorithm (yourself) in machine instruction (binary numbers)
How to write an algorithm that a computer can execute (cont.) • The easy way: • Write the algorithm (yourself) in a programming language (using an editor like gedit) • Translate the program using a compiler into machine instructions (The translated program is stored in a computer file) • Let the computer execute the translated program in machine instructions (This translated program is an computer application because it can be run by a computer)
How to write an algorithm that a computer can execute (cont.) • Pictorially: • (An application is a file that contains machine instructions (which can be executed by a computer))
Using the Java compiler • The name of the Java compiler (application) is: • javac
Using the Java compiler (cont.) • A Java program (= a computer algorithm written in the Java programming language) is first written and stored in a file The name of the file must end with the characters: The ending of a filename is called the file extension and it is commonly used to identify the kind of file. • .java
Using the Java compiler (cont.) • The following command will translate a Java program into machine code: The Java compiler javac will store the translated machine codes into a file name ProgramFileName.class • UNIX prompt>> javac ProgramFileName.java
Using the Java compiler (cont.) • Example:
Using the Java compiler (cont.) • Example Program: (Demo above example) • The Hello.java Prog file: http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/02/Progs/Hello.java • What to do: • Right click of the link and save in some scratch directory • Change current directory to that scratch directory • Compile Hello.java with javac Hello.java
The Java machine language • The translated machine code produced by the Java compiler javac is called: • Java bytecode
The Java machine language • Java bytecodes are executed by a Java machine --- a computer that executes machine instructions in Java bytecodes However, no such Java machine has ever been built In other words: • There does not exist a physical computer (machine) that can execute Java byte codes
The Java virtual machine • Emulators: Emulators are also known as virtual machines (Because they do the same thing as a real machine, but is not a physical machine) • Emulators are (special) computer programs that emulate the exact behavior of a machine or entity
The Java virtual machine (cont.) • Java virtual machine: (A Java virtual machine is an emulator) • A Java virtual machine is a computer program (application) than can execute an algorithm written in Java bytecode
The Java virtual machine (cont.) • The name of this computer application is: • java
Executing the translated Java program with a Java virtual machine • Suppose the program containing the Java bytecode is called: This program can be executed by the Java virtual machine java by executing the following command: ProgramFileName.class java ProgramFileName
Executing the translated Java program with a Java virtual machine (cont.) • Note: • You must omit the extension .class • The Java virtual machine java will append the extension .class to its argument
Executing the translated Java program with a Java virtual machine (cont.) • Example:
Executing the translated Java program with a Java virtual machine (cont.) • Example Program: (Demo above code) • Hello.java Prog file: http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/02/Progs/Hello.java • What to do: • Right click on the link ans save in some scratch directory • Change current directory to that scratch directory • Compile Hello.java with: &nsbp; javac Hello.java • Then execute the Java bytecode Hello.class with: java Hello