VOOZH about

URL: https://www.informit.com/articles/article.aspx?p=3222409

⇱ Fundamental Programming Structures in Java | 3.1. A Simple Java Program | InformIT


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:


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

The program declares a method called . The term “method” is Java-speak for a function—a block of code that carries out a specific task. You must have a method in every program. You can, of course, add your own methods and call them from the 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 method calls another method, called , defined in the 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 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 , is a method of a class .

The standard naming convention (used in the name ) 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.”

Prior to Java 25, you had to explicitly declare the class containing the method. This is no longer necessary.

It used to be a requirement to declare the method as


You will learn in Chapter 4 what the keywords and mean. The parameter holds command line arguments—see Section 3.10.5.

Moreover, Java 25 introduced the class to simplify console input and output. Previously, you had to use the special object, which was yet another concept that was confusing to beginners.

To run any of the programs in this chapter with an older version of Java, do the following:

  1. Place all code inside a class whose name equals the file name, without the extension.

  2. Declare in the old style.

  3. Replace with

For example:


Version 1.0 of the Java Language Specification decreed that the method must be declared , , and . (The Java Language Specification is the official document that describes the Java language. You can view or download it from https://docs.oracle.com/javase/specs.)

However, early versions of the Java launcher were willing to execute Java programs even when the method was not . A programmer filed a bug report. To see it, visit https://bugs.openjdk.org/browse/JDK-4252539. In 1999, that bug was marked as “closed, will not be fixed.” An engineer added an explanation that the Java Virtual Machine Specification does not mandate that is and that “fixing it will cause potential troubles.” In the end, sanity prevailed. As of Java 1.4, the Java launcher enforces that the method is , as intended in the language specification. That behavior was in place until Java 25, which allows other forms of the method.

It is remarkable that the bug reports and their resolutions have been available for anyone to scrutinize for as long as Java existed, even before it became open source.

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


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 method that is declared in a class called . Notice the period that separates the name of the class and the method.

The 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 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 method with no arguments just prints a blank line. You invoke it with the call


The class also has a method that doesn’t add a newline character to the output. For example, prints without a newline. The next output appears immediately after the letter .

You compile the file with the command

You run the sample program with this command:


When the program executes, it simply displays the string on the console.

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


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 and stores it in the same directory as the source file. Whenever you want to launch the program, issue the following command:


Remember to leave off the extension.

When you use

 ClassName

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

books, eBooks, and digital learning
© 2026 Pearson. All rights reserved, including those for text and data mining and training of artificial intelligence and similar technologies.
👁 Pearson Logo