The amazing adventures of Doug Hughes

What is Java?

This is an article I wrote for my Java class at The Iron Yard. It’s being published here with permission.


Writing Software in Java

To write a program is to “speak” in a language a computer can understand. We don’t actually “speak” to a computer, but we can write messages that it can understand. These words aren’t in a natural language, though. Instead, they’re in a programming language.

My class at The Iron Yard is primarily about the programming language Java. Java is just one of hundreds of programming languages, but it has the distinction of being the most popular.

To a fresh eye, programming languages often look like gibberish. For example:

int total = scores.stream().mapToInt(Scoreable::getScore).sum();
return total / scores.size();

This Java code could be translated into english as something like, “Calculate the average score from a set of scores.” This example is intended to illustrate a point: Java isn’t english.

What is Java anyhow?

We should probably delve into what exactly Java is. This isn’t a simple question and could quite quickly lead us down the proverbial rabbit hole. Instead, I’ll focus on the most important points.

Java is a programming language

In English we know that sentences start with capitol letters and end with periods, question marks, or exclamation points. We know the basic sentence structure of “subject verb noun”. Sentences can be collected together to communicate complex messages. This is the syntax of English. All human languages have their own syntax.

All programming languages also have their own syntax. Java code is written in Java’s syntax. Unlike spoken languages, a programming language’s rules are very strict. In English, a missing comma isn’t the end of the world. The reader can usually intuit what a writer is trying to communicate, even if their grammar isn’t perfect. However, computers do not have intuition. They require very precise and particular instructions, communicated in exactly the right manner. Missing even a single semicolon in Java will prevent your program from working at all.

The programming language Java is the syntax you use to tell the computer what you want it to do.

This is an example of the most basic Java program. When compiled and run (more on this in a bit) it will print out the words “Hello, world!”.

public class HelloWorld {

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

}

This file must be named HelloWorld.java for it to compile.

5b77edad-screen20shot202016-04-0420at2012-52-1920pm

What is a compiler?

Computers only speak one language: Binary. Ones and zeros. That’s it. A CPU receives instructions in binary and does exactly what the instructions tell it to do. Unfortunately, humans don’t speak binary. Perhaps we could memorize it?

Back in the dark ages of programming, this is essentially what programmers did. Mnemonics, known as assembly code, were created that humans could more easily memorize. A programmer would write assembly code and later manually translate it to binary.

aeef70ab-example20assembly20and20binary20instructions

No mere mortal wants to manually translate assembly code to binary. Good programmers are often lazy, and so eventually programs were created to automatically translate assembly code into binary instructions. These were the first compilers.

A compiler takes source code written in a programming language and translates it into binary code. Over time, compilers matured and added features that led to modern programming languages.

But, there’s a catch! Different CPUs speak different binary languages! This means that we need different compilers for different CPUs. Mix in the complexities of supporting different operating systems, and it becomes very difficult to write a program that is cross platform. A cross platform program is one that can be run on multiple operating systems and CPU architectures.

This is why there so many applications that only run on one platform. An application written to run on iOS on an iPhone can’t work on Android, much less Windows.

The Java compiler

The Java compiler is a compiler. Surprise! As with any compiler it translates Java source code into binary instructions.

The command line program we use to compile java code is is javac. This can be executed on the command line like this:

8a92bce7-screen20shot202016-04-0420at2012-54-0420pm

The result of running the Java compiler on a .java file is a new file with the same name, but with a .class extension. The .class file is what contains the binary code.

The Java compiler comes with the Java Development Kit, which must be downloaded and installed before Java programs can be compiled and run.

However, unlike traditional compilers that write binary instructions for a particular CPU and OS, the Java compiler writes its own dialect of binary called Java bytecode. Java bytecode isn’t created for any particular CPU or OS. By itself, Java bytecode is useless. We need something else to make it useful.

The Java Runtime Environment

Since a Java program is not compiled for a specific CPU or OS, we can’t simply execute it like any other program. Instead, we need a Java Runtime Environment (JRE). This is the “Java” that your computer is constantly bugging you to update.

221f36d2-java20update20available

The JRE is actually a virtual computer. It simulates the CPU and hardware of a real computer. Just like any other CPU, Java’s virtual CPU has its own instruction set. This instruction set is Java bytecode! As the JRE executes your program, it actually compiles the Java bytecode into CPU-specific binary instructions that your computer can execute. This process is called Just in Time (JIT) compiling. A handy side effect of this is that Java bytecode can be executed anywhere the JRE can be installed, regardless of hardware or operating system.

You can run a compiled Java .class file using the java command line program. The syntax is:

java <Class Name without .class extension>

For example:

3bc2ee75-running20java

Through this process of JIT compiling, the JRE effectively hides from the programmer everything that makes a particular CPU or OS unique. The computer itself becomes a black box to a Java developer. We no longer care what CPU or OS we’re using. We no longer care about the implementation, we only care that it works. The JRE takes care of everything else for us.

This process of hiding implementation details is called abstraction. Abstraction is a key theme in programming.

The Java Development Kit

The Java Development Kit is key to all of the above. It provides everything we need to make Java applications. In particular, the JDK provides the javac command we use to compile Java code.

The JDK also includes the JRE, which provides the java command we use to execute Java bytecode. Many other useful tools are included for debugging, analyzing code, and packaging applications for distribution.

So, what is Java?

Java is a collection of tools used to compile and execute code written in the Java programming language.

Tag Cloud