Java 101 – The Basics
As stated in the intro to these articles, this content was designed to use in a Java bootcamp environment. That means ideas and concepts are presented quickly in a compressed format. This allows students to get hands on with building code quickly instead of spending too much time focusing on the hows and whys. Of course there are pros and cons to this approach, as there are with most things related to computer software. With the disclaimer out of the way, it is time to dive into some basics.
Hello World
The first step in any introduction to a new language is to create a Hello World program or script. Here is one in Java.
package edu.uca.aca2016.basics;
/**
*
* @author Chad Files
*/
public class HelloWorld {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
System.out.println("Hello World");
}
}
When this program is executed through the Java Virtual Machine, it will simply print the words Hello World in the terminal. That is all it does, but it is a good place to start in order to understand some of the basics of Java.
The first line in the program includes a package
statement. This is the fundamental way Java organizes code and protects against conflicts with classes that are named the same. In this program, the package is edu.uca.aca2016.basics
. That means the Java source file for this program resides in at the end of a specific directory path on the physical development machine. That looks something like this.
/home/cfiles/projects/aca2016/src/
└── edu
└── uca
└── aca2016
├── basics
│ ├── HelloWorld.java
In this example, the root directory of the project is /home/cfiles/projects/aca2016/src
and the edu/uca/aca2016
basics matches the package path. The next thing to point out is every Java source code file is built as a class
. This is a term taken from Object Oriented Programming where every program source file represents an object. In Java, the objects are called classes and this one is named HelloWorld
. This also relates to how the files are named. Java requires that the file name match the class name exactly. For that reason, the file for the HelloWorld
class is named HelloWorld.java
.
In this example, there are also comment blocks. Comment blocks start with /*
and end with */
. Anything between these characters is ignored by the compiler as a comment. Comments that use this syntax can span multiple lines and are often used to provide detailed in-depth comments. In the code example there is an extra *
after the opening of the comment and one on ever line. This is special syntax for comments that is called Javadoc. When used correctly, it allows the author of the code to provide code comments that can be compiled directly into formatted documentation. Java also supports single line comments that are indicated by the //
characters. These comments are handy for in-line notes that are specific to a few lines of code. It should be noted, comments can also be used to hide blocks of code from the compiler during development.
There are several other things to point out that will be covered down the road. The keywords public
, static
, void
and others will be covered in a later discussion on scoping and typing. For now, just note they are there and important and required.
Compiling and Running
In the above example, there is one method in the class: main
. This is a special reserved method within Java. It identifies the class to the compiler and interpreter as being an executable entry point. Meaning, the class can be executed as a program. Without this method, it would not be possible to run this class as a program.
Without digging too deep, running a Java program has two steps. First the source must be complied, then the compiled code has to be executed. A feature that makes Java portable is its ability to run within a virtual machine as byte code. This means, compiled Java code is interpreted instead of executed natively. In most cases, this allows Java to execute on Windows, MacOS, Linux, and other operating systems without having to recompile the source for every platform. It is compiled once into byte code and the system specific Java Virtual Machine (JVM) executes the code.
To compile the source code above, the following command can be used:
$ javac edu/uca/aca2016/basics/HelloWorld.java
The directory where HelloWorld.java
is located, now contains a new file named HelloWorld.class
. This is the compiled byte code from the source.
The program can now be executed with the JVM using the command:
$ java edu/uca/aca2016/basics/HelloWorld
This should produce the output:
cfiles@machine:~/projects/aca2016/src$ java edu/uca/aca2016/basics/HelloWorld
Hello World
The line of code that contains System.out.println
tells Java to print the string Hello World and that is what it did. Pretty neat stuff, even if not terribly useful. The coming installments will take this basic example and expand it into something far more useful.