Programming Java

An Introduction to Computer Science



2. Computer Program

Programming Languages

A computer language or a programming language is the language you use to write instructions to a computer system. There are numerous programming languages: C++, Java, C#, Python, JavaScript, and Ruby, etc. All of these are high-level languages.

A high-level language has high-level of abstraction from the inner workings of a computer system. This means you don’t write code to interact with the hardware components or the software of a computer system. High-level languages provide programs that do this for you.

A low-level language has low-level of abstraction from the inner workings of a computer system. Machine language or machine code is a low-level language. It’s written in 0s and 1s (zeros and ones) which the hardware can interpret directly.

A computer system doesn’t understand high-level languages. It only understands machine code. Therefore, a computer program written in a high-level language must be translated to a machine code before it’s executed.

Compiled Languages

There are different methods to convert from high-level language to machine code. The languages that use a compiler program for conversion are generally called “compiled languages”.

A compiler is a program that takes a source code as input, translates it to a machine code, and outputs a file called an “object code”. If a program consists of many source codes, all of these are converted to object codes. The collection of object codes is linked together into a single file called “executable”. The operating system runs the executable file by loading it into the main memory and executing the instructions in the object codes.

Figure 2A – The Compile-Link-Execute Process

The compile-link-execute process is system dependent because the object code (i.e., machine code) is different from system to system. Also, if the source code contains any system specific instructions, it itself is system dependent.

Interpreted Languages

The languages that use an interpreter program for conversion are generally called “interpreted languages”. An interpreter is a program that converts a high-level language statement to a machine code statement and executes it as the program is running. Think of it as a simultaneous translator. This results in a slower execution compared to the compiled programs.

How Java Works

Java is a hybrid language in that it uses both compiler and interpreter in its own way. JavaC is the compiler for Java programs. It takes Java source code as input and outputs a file called bytecode. The bytecode is the machine code understood by Java Virtual Machine (JVM). JVM includes an interpreter program that executes the instructions in the bytecode and addresses any system specific operations.

Java Virtual Machine (JVM) is a program that runs on top of an operating system. It serves two purposes: (1) run Java program and (2) manage the memory used by the program. JVM removes the system dependency because the same bytecode can be used on any system as long as that system has JVM installed.

Figure 2B – Java Virtual Machine

Code Example


Running a Java Program

We’ll demonstrate how a Java program is compiled and executed using a local computer that has Java Development Environment setup. We can’t use the online tool Repl.it because Repl.it automates the compilation and interpretation processes, and the steps are hidden from the user.

The demonstration follows three steps:

  1. Create source code
  2. Compile source code
  3. Execute bytecode

Step 1: We create an empty file and name it “Main.java”. This is our source code. Java source code uses the file extension .java. We copy and paste the following sample program and then save the file.

class Main {
  public static void main(String[] args) {
    System.out.println("Hello world!");
  }
}

Step 2: From command prompt (Windows) or terminal window (macOS), we enter the following command. Notice the command javac. That’s the name of the Java compiler.

> javac Main.java

The javac command compiles “Main.java” source code and outputs a bytecode file named “Main.class”.

Step 3: From the same command prompt or terminal window, we enter the next command: java.

> javac Main.java

> java Main

This java command loads “Main.class” bytecode to Java Virtual Machine (JVM) and executes the instruction. The instruction tells the system to output “Hello world!” to the screen.

> javac Main.java

> java Main

Hello world!

After this execution, the program ends.

NOTE: The source code should be saved and re-compiled every time its content changes. Otherwise, the java command executes the old code.


2022 Copyright


By:


Design a site like this with WordPress.com
Get started