Skip to main content
Logo image

Problem Solving with Algorithms and Data Structures using Java: The Interactive Edition

Section 1.10 Writing a Complete Java Program

Up to this point, we have been doing all of our work in jshell. At some point, we will have to write a complete Java program that we can run from an Integrated Development Environment (IDE) or the command line.
Unlike other languages, such as Python, which don’t require a lot of setup to produce a program, Java is exceptionally verbose. Our plan is to present the information you need to set up a program without going into a long, laborious explanation. We’re giving you a “magic spell”; recite this incantation and your program will work.
Here’s the minimal prototypical Java program:
Let’s go through this line by line.
Line 1: Every program is in a Java class (we will see more of this in Section 1.15). By convention, class names begin with an upper case letter. When you save this program, the file name must be Java is case sensitive, so upper and lower case names are different. Using a file name like exampleprogram.JAVA will not work properly!
The opening brace { at the end of the line starts a block of Java statements; the corresponding closing brace on line 6 ends the block.
Line 3: This is the “magic spell” portion. Every program that you want to run must have a method named main; the operating system will start running your program by calling this method. Its argument is an array of String. When you run a Java program from the command line, any arguments you put after the program name will be stored in this argument array. By convention, the array name is args, but you may name it anything you wish.
The words public static control access to the method. The keyword void specifies that this method does not return any value.
The opening brace at the end of the line starts a new block, which contains...
Line 4: The method body, which, in this case, consists of a single statement that prints the words Java works!. As with all Java statements, it ends with a semicolon. Read from right to left, the println method is a method of the out object (your terminal) in the System class. This method prints its argument followed by a newline—ln stands for line.

Subsection 1.10.1 Running a Java Program

Most Integrated Development Environments are set up so that you can compile and run your Java programs with the click of a button or a menu choice. It is also possible to run a Java program from the command line. Here is an example of running the preceding program from a Linux shell where the $ is the command line input prompt:
$ javac
$ java ExampleProgram
Java works!
$ ls
The javac command invokes the Java compiler. It translates the source code into bytecode, a form which the Java runtime system can execute. Since there are no errors in the program, the command does not give any output (“no news is good news”).
The java commad invokes the runtime; you give it the name of the class to run (in this case, ExampleProgram).
The ls command lists the files in the current directory; you don’t need to do this command, but it’s here to show you that, after compiling, you have two files: ExampleProgram.class (the bytecode) and (the source code).
You have attempted of activities on this page.