Lesson Weekend

Now that we understand some Java basics, let's construct something a little larger! In this lesson we'll discuss how to setup, compile, and run a basic Java program.

Application Setup

Every program must have a .java file containing this basic setup:

sample-project/MyProgram.java
public class MyProgram {
  public static void main(String[] args) { //I am in charge here
 }
}

This will be nearly identical in every Java application we build. Only the MyProgram name will change from application to application. This word must be capitalized, and must match the name of the file. As you can see, the file depicted above is MyProgram.java, so the class name is MyProgram.

The application's code will reside within the curly brackets for the main() method. This is method that will run automatically when we launch our program.

Hello World

Let's create the classic "Hello World" application together, to see how this works.

Setup

Create a new project directory called hello-world according to the instructions described in "Getting Started with IntelliJ". In the src/main/java directory, right click and make a new java class called Hello.java. Keep the uppercase! Open it in your text editor and place the following inside:

src/main/java/Hello.java
public class Hello {
  public static void main(String[] args) {
    System.out.println("Hello World");
  }
}

System.out.println() is a method we can use to print to the console. It simply takes a String argument and prints its contents to the console. Easy enough!

Compiling and Running a Program

Next, we'll run this code by doing the following:

Choose "Run" from the main menu, then "Run" again. Then, choose "Hello".

run-hello

After running this command, we should see the line "Hello World" appear in our console at the bottom! Congratulations, you just wrote your first Java program!

Compilation

Great! But let's back up for a moment: What is this command actually doing?

Believe it or not, our computer won't automatically understand the Java we've written in our Hello.java file!

Before we can run our program we need to compile it into something the computer can understand. Compiling takes our source code (the code we've written in Hello.java) and translates it into something called bytecode.

Bytecode is a special type of machine code meant to run on the Java Virtual Machine (JVM). You don't need to know how to write or read bytecode. In fact, it's pretty indecipherable to humans. Our compiler will always handle translating the source code we write in our .java files into bytecode for us. These are the .class files you will see after compiling. Feel free to open one in atom and take a peek. You'll find them in your build directory.

The Java Virtual Machine (commonly referred to as JVM, for short) that runs this bytecode is already included as part of our Java Runtime Environment (JRE). Additionally, the compiler to translate source code into bytecode is included in the Java Development Kit we've already downloaded, too!

Why Compile?

But why does Java need to be compiled when other languages don't? When we learned JavaScript in Intro to Programming we could simply link our JavaScript code to our webpages with a <script> tag, and everything worked!

The short answer is simply because JavaScript is an interpreted language, and Java is a compiled language. An interpreted language has source code that may be executed directly, whereas a compiled language must have its source code compiled into machine code before it can run.

The more complex answer is that JavaScript is actually compiled, too! It's just not compiled by us. We don't have to explicitly instruct it to compile in the same way we must explicitly instruct Java. Web browsers contain JavaScript engines. When our scripts are run, these engines do translate JavaScript source code into machine code. It just happens behind the scenes, as the code is running.

But don't worry about differences between how languages are executed, or memorizing these processes. You only need to remember the following: Programming languages must be translated into machine code before computers can understand them. While some languages have tools that handle this automatically, Java must be explicitly compiled before it can run. And we need to use a command to manually tell Java to compile.

Result of Compiling

If we open the Hello.class file, we can see the bytecode created by the compiler. It's pretty illegible to human eyes, but that's alright! We won't need to work with this code ourselves, this is for the computer:

hello-world/Hello.class
����4"

<init>()VCodeLineNumberTableLocalVariableTablethisLHello;main([Ljava/lang/String;)Vargs[Ljava/lang/String;
SourceFile
Hello.java
Hello World !Hellojava/lang/Objectjava/lang/SystemoutLjava/io/PrintStream;java/io/PrintStreamprintln(Ljava/lang/String;)V!  /*��



        7   ���

Remember:

  • Files with .java extensions contain your source code. This is the code written by the developer. It's more readable to human eyes.
  • Files with.class extensions contain compiled bytecode. This is the source code the compiler has translated into bytecode. This code will be run in our JVM. You do not need to alter or work in this file.