Learn Java from the ground up

Get an overview of the Java platform and the tools you'll need to code your first Java application

1 2 Page 2
Page 2 of 2

Java installation and setup

The Java platform is distributed as the Java Runtime Environment (JRE), which contains the JVM, the standard class library, and a few other items. You will need both the JRE and a JDK in order to develop and run Java programs. The JDK download from Oracle includes the JRE and the basic development tools required to begin developing, debugging, and monitoring your applications in Java. At the time of this writing the most current version of the JDK is Java 12.

After downloading and installing the JDK you should update your PATH environment variable to reference the JDK's bin subdirectory of the installation directory, so that you can execute JDK tools from any directory in the file system. If you need instructions for updating PATH you can find them here. (Note that my examples are based on using the command line with command-line Java tools, but you could use a Java IDE if you prefer.)

The JDK installation directory contains various files and subdirectories, including the following three important subdirectories:

  • bin contains various JDK tools, such as the Java compiler (javac), the Java application launcher (java), and the Java shell (jshell). You'll interact with these and other tools throughout the Java 101 series. (Note that the Java compiler and the JIT compiler are two different compilers.)
  • jre contains the JDK's private copy of the Java Runtime Environment, which lets you run Java programs without having to download and install the standalone JRE.
  • lib contains library files that are used by JDK tools. For example, tools.jar contains the Java compiler's class files -- the compiler is a Java application. (The javac tool isn't the compiler, but is a native-platform-specific convenience for starting the JVM and running the Java-based compiler.)

Once you've installed the JDK and configured your development environment, you are ready to code your first Java application.

Write your first Java application

Most examples in the Java 101 series are presented in the form of applications. An application is minimally implemented as a single class that declares a main() method, as follows:

class X
{
   public static void main(String[] args)
   {
   }
}

Think of a class as a placeholder for declaring methods and data item storage locations. The class declaration begins with the reserved word class, which is followed by a mandatory name, which is represented by X, a placeholder for an actual name (e.g., Account). The name is followed by a body of methods and data item storage locations; the body is delimited by open brace ({) and close brace (}) characters.

Think of a method as a named block of code that processes inputs and returns an output. main() receives an array of String objects describing its inputs; the array is named args. Each object identifies a string, a double-quoted sequence of characters that (in this case) denotes a command-line argument, such as a file's name passed to the application as one of its arguments. main() doesn't return an output, and so it is assigned the void reserved word as its return type.

Additionally, main()'s header is assigned public and static so that it can be called by the java application launcher. Following this method header is a body of code; as with a class body, the method body is delimited by brace characters.

This is all you need to know about classes and methods (especially main()) in order to code your first Java application. You'll learn more about these language features (along with strings, arrays, return types, and more) in future articles.

Say hello

It's traditional to introduce a computer language by presenting a program that outputs the famous "hello, world" message. You can see this in Listing 1.

Listing 1. HelloWorld.java (version 1)

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

The application's class is named HelloWorld. Its main() method executes System.out.println("hello, world"); to send the contents of the "hello, world" string to the standard output stream, which is typically the command line.

Store Listing 1 in a file named HelloWorld.java. Then, at the command line, execute the following command to compile this source file:

javac HelloWorld.java

Note that javac requires the .java file extension; otherwise, it generates an error message. If the source code compiles without an error, you should observe HelloWorld.class in the current directory.

HelloWorld.class contains the executable equivalent of HelloWorld.java. To run this class file via the java application launcher tool, execute the following command:

java HelloWorld

Note that java doesn't permit you to include the .class file extension; if you do so it will generate an error message.

Assuming you've written your program correctly, you should observe the following output:

hello, world

If you see this output, congratulate yourself: You've just compiled and run your first Java application! There will be many more examples throughout the rest of this series.

Personalizing hello

We can improve on Listing 1 by personalizing the application. For example, we might want to output hello, Java instead of hello, world. Listing 2 enhances the original program:

Listing 2. HelloWorld.java (version 2)

class HelloWorld
{
   public static void main(String[] args)
   {
      System.out.println("hello, " + args[0]);
   }
}

Listing 2 shortens "hello, world" to "hello, " and appends + args[0] to join the args array's first string to the message. The result is then output.

"hello, " + args[0] is an expression that appends the string in the first element of the args array to hello, . (You'll learn more about expressions and this string concatenation later in the Java 101 series.)

Compile Listing 2 (javac HelloWorld.java) and run the application with a single command-line argument, as follows:

java HelloWorld Java

You should observe the following output:

hello, Java

Java exceptions

Now suppose you execute HelloWorld without any command-line arguments, as in java HelloWorld. In this case you would receive an exception:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
    at HelloWorld.main(HelloWorld.java:5)

What you see is an error message referring to an exceptional condition in your code. Specifically, because there are no command-line arguments, args[0] doesn't contain anything. The attempt to access args[0]'s non-existent string is illegal.

As you develop Java applications, you'll run into many messages like this one. Rather than be intimidated, think of these messages as tips for correcting problems.

Get started with Java shell (jshell)

Java SE 9 introduced Java shell (jshell) as an alternative to the Java compiler (javac) and Java application launcher (java) tools. This interactive tool is very convenient for learning the Java language and APIs, and for prototyping Java code.

With jshell, you don't have to write a complete application, compile it, and then run it just to try out a language feature or API. Instead, you can enter and then immediately execute a snippet (one or more lines of Java code), and observe the results.

Here's how to execute jshell from the command line:

C:\temp>jshell

The tool responds with preamble text and a prompt:

|  Welcome to JShell -- Version 12
|  For an introduction type: /help intro

jshell>

You can then enter a snippet or a command at the prompt. To differentiate a command from a snippet, you must precede the command name with a forward slash (/) character. For example, try seeing what happens when you enter /help.

In Listing 1 you saw a classic HelloWorld application. Here's the jshell equivalent:

jshell> System.out.println("hello, world")
hello, world

jshell>

In this case, all we have to do is specify System.out.println("hello, world".) (We don't need a semicolon [;] terminator in this case.) As soon as we press Enter, jshell executes the code and reveals the output.

When we're ready to exit jshell, we simply type /exit at the prompt, and then press Enter. Java shell will immediately return to the operating system command prompt.

There's a lot more to learn about jshell, and we'll practice with it throughout the Java 101 series.

In conclusion

We've covered a lot of ground in this tutorial. You've learned that Java is a language and a platform, and that there are three editions of Java. You know how the JVM executes Java class files. You've learned the difference between the JRE and the JDK, and how to set up the JDK on your system. You've gained insight into the architecture of a Java application and learned how to compile source code and execute class files using javac and java, and with the newer jshell utility.

While Java is an object-oriented language, not all of its features are object-oriented. In the next turorial you'll learn how to use identifiers, types, literals, and variables in your Java programs. You'll also learn why and how to document your code, and you'll see how Java's support for Unicode affects source code and compilation.

This story, "Learn Java from the ground up" was originally published by JavaWorld.

Copyright © 2019 IDG Communications, Inc.

1 2 Page 2
Page 2 of 2
How to choose a low-code development platform