Writing, Compiling, and Running a Java Program

In this lesson, we will be looking at the steps taken to write and execute a Java program.

Existing programs

We will be able to execute many of the programs we develop as examples in this course. As you have seen in such cases, a RUN button appears near the code. Clicking it compiles and executes the program. For some programs, you will provide input data by first clicking in the window that appears and then typing the required input data.

New programs

Sometimes, you will actually write portions of or entire programs. As you type Java statements within one of the provided areas, the characters are stored in the computer’s primary memory. Lines of code are numbered automatically. These numbers enable us to talk about particular lines, but they are not a part of the Java statements.

Fix errors

Once you have your program typed, you will compile it by clicking the RUN button. More than likely, the compiler will complain about something you typed in your program! Mistakes in your program that the compiler detects are known as syntax errors. Maybe you made a simple typing mistake, or perhaps you didn’t follow Java’s rules exactly. The compiler will not try to fix your errors, but it will give you an opinion about what seems to be wrong.

After fixing your mistakes, you should try to compile the program again. Several repetitions of compiling and fixing mistakes may be necessary before the compiler actually produces bytecode and your program executes.

Check output

Did the program do what you expected and planned? Once you are able to run your program, you should test to see whether it behaves as it should. You might need to correct errors in the program’s logic and repeat the compilation, execution, and testing phases. We will take some time in this course within the Debugging Interludes to see how to find programming mistakes.

The figure given below illustrates the general steps one takes in writing Java programs, either in this course or after the course. The appendix gives more guidance for creating and executing Java programs after you finish this course.

Get hands-on with 1400+ tech skills courses.