Between the Braces

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.

Answer:

  1. What should the source file be named?
    • AddUpNumbers.java
  2. What will the compiler name the bytecode file?
    • AddUpNumbers.class

Between the Braces

The small Java programs in this chapter will all look something like this:

class Hello
{

}

Everything that a program does is described between the first brace and the final brace of a class. To start with, we will have only one class per source code file, but in later chapters there may be several classes per source code file.

class Hello
{
  public static void main ( String[] args )
  {
    System.out.println("Hello World!");
  }
}

The example program writes Hello World! to the monitor. This looks like a lot of work for such a little task! But, usually, programs are much longer and the details you see here help to keep them organized. The line

public static void main ( String[] args )

shows where the program starts running. The word main means that this is the main method — where the Java virtual machine starts running the program. The main method must start with this line, and all of its parts must be present. You can put more spaces in than shown here, and the spaces surrounding the parentheses and square brackets are optional.

QUESTION 3:

Would the following be an acceptable start for a main method:

public    static void main(String[] args)