Components of Java Programs

What is displaying in this program?

As you saw when the program ran, it displayed:

Hello, World!

Notice that the output does not include the quotation marks that you typed in the code.

Java programs are made up of class and method definitions, and methods are made up of statements. A statement is a line of code that performs a basic action. In the HelloPrinter program, this line is a print statement that displays a message to the user:

System.out.println("Hello, World!");

System.out.println displays results on the screen or the console; the name println stands for “print line”. Like most Java statements, the print statement ends with a semicolon (;).

Java is “case-sensitive”, which means that uppercase and lowercase are not the same. In the HelloPrinter program, System has to begin with an uppercase letter; system and SYSTEM won’t work.

The main method

method is a named sequence of statements. This program defines one method named main:

public static void main(String[] args){

The name and format of the main method is special. When the program runs, the JVM immediately starts looking for the main method so that it knows where to start running the program. It is this very special method that signals to the compiler that this is where the program begins. It starts at the first statement in main and continues on to run the program. In this program, it ends when it finishes the last statement.

The class name

This program defines a class named HelloPrinter. For now, a class is a collection of methods. You can give a class any name you like, but it is Java convention to start with a capital letter. The name of the class has to match the name of the file it is in, so this class has to be in a file named HelloPrinter.java.

Again, remember the Java conventions for naming classes. They should be nouns with capital letters to start each word.

Good Class Names

  • Building
  • Employee
  • Event
  • AddressBook

Bad Class Names

  • building
  • address book
  • Address_Book
  • thing

Curly Brackets

Java uses curly brackets ({ and }) to group things together. In HelloPrinter.java, the outermost brackets contain the class definition (public class HelloPrinter), and the inner braces contain the method definition (public static void main(String[] args)). The IDE will help you match these curly brackets to ensure that you have everything closed. Note the indention to assist with this. There are other built-in functionality too so you can determine which curly brackets closes which method or class.

Up Next: How Java Programs Run