Home > Articles

Fundamental Programming Structures in Java

This chapter is from the book

At this point, you should have successfully installed the JDK and executed the sample programs from Chapter 2. It’s time to start programming. This chapter shows you how the basic programming concepts such as data types, branches, and loops are implemented in Java.

3.1. A Simple Java Program

Let’s look more closely at one of the simplest Java programs you can have—one that merely prints a message to console:

void main() {
    IO.println("We will not use 'Hello, World!'");
}

First and foremost, Java is case sensitive. If you made any mistakes in capitalization (such as typing Main instead of main), the program will not run.

The program declares a method called main. The term “method” is Java-speak for a function—a block of code that carries out a specific task. You must have a main method in every program. You can, of course, add your own methods and call them from the main method.

Notice the braces { } in the source code. In Java, as in C/C++, braces are used to form a group of statements (called a block). In Java, the code for any method must be started by an opening brace { and ended by a closing brace }.

Brace styles have inspired an inordinate amount of useless controversy. This book follows a compact style that is common among Java programmers, sometimes called the “Kernighan and Ritche” style. In other styles, matching braces line up. As whitespace is irrelevant to the Java compiler, you can use whatever brace style you like.

The main method calls another method, called println, defined in the IO class. You will learn a lot more about classes in the next chapter. For now, think of a class as a container for the program logic that defines the behavior of an application. Classes are the building blocks with which all Java applications are built.

In fact, everything in a Java program lives inside a class, even our main method. It is placed inside a class whose name is the name of the file, without the extension. If we place the code in a file named FirstSample.java, main is a method of a class FirstSample.

The standard naming convention (used in the name FirstSample) is that class names are nouns that start with an uppercase letter. If a name consists of multiple words, use an initial uppercase letter in each of the words. This use of uppercase letters in the middle of a name is sometimes called “camel case” or, self-referentially, “CamelCase.”

Now turn your attention to the contents inside the braces of the main method,

IO.println("We will not use 'Hello, World!'");

This is the body of the method. The body of most methods contains multiple statements, but here we have just one. As with most programming languages, you can think of Java statements as sentences of the language. In Java, every statement must end with a semicolon. In particular, carriage returns do not mark the end of a statement, so statements can span multiple lines if need be.

Here, we are calling the println method that is declared in a class called IO. Notice the period that separates the name of the IO class and the println method.

The println method receives a string argument. The method displays the string argument on the console. It then terminates the output line, so that each call to println displays its output on a new line. Notice that Java, like C/C++, uses double quotes to delimit strings. (You can find more information about strings later in this chapter.)

Methods in Java, like functions in any programming language, can use zero, one, or more arguments, which are enclosed in parentheses. Even if a method has no arguments, you must still use empty parentheses. For example, a variant of the println method with no arguments just prints a blank line. You invoke it with the call

IO.println();

You compile the file with the command

You run the sample program with this command:

java FirstSample.java

When the program executes, it simply displays the string We will not use 'Hello, World!' on the console.

If you intend to run a program multiple times, it is more efficient to compile it first:

javac FirstSample.java

You end up with a file containing the bytecodes for this class. These are instructions for the Java virtual machine. The Java compiler names the bytecode file FirstSample.class and stores it in the same directory as the source file. Whenever you want to launch the program, issue the following command:

java FirstSample

Remember to leave off the .class extension.

When you use

java ClassName

to run a compiled program, the Java virtual machine is launched, and execution starts with the code in the main method of the class you indicate.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.