Getting Started with Java Programming

Validating the Installation of Java

Your browser needs to be JavaScript capable to view this video

Try reloading this page, or reviewing your browser settings

This video segment presents a simple Java program that should it run will validate that your Java installation is correct. A first introduction to a Java class is also part of this segment.

Keywords

  • compile
  • bytecode
  • class
  • object
  • output
  • main

About this video

Author(s)
Kenneth Fogel
First online
02 July 2022
DOI
https://doi.org/10.1007/978-1-4842-8614-2_3
Online ISBN
978-1-4842-8614-2
Publisher
Apress
Copyright information
© Ken Fogel 2022

Video Transcript

With Java successfully installed, it’s now time to write a small program to validate that all is ready. We will be working with a text editor and a command prompt window in Windows. While the installation instructions were just for Windows, everything except for the two set commands will work on Linux or Mac OS computers.

The first thing you need to do is to create a folder on your drive where the Java programs you write will be stored. When you write code files with your text editor, you must save the files in this folder. Now open your command prompt window. Some operating systems may call this the console or the terminal. On Windows, you want to run the program cmd.exe.

Use the Windows key plus the R key to open a run dialog and enter “cmd” in the box and click OK. A command prompt window will open. Enter the set JAVA_HOME and set path if you are using the zip file install of Java as you see on the screen. Now use the change directory command, CD, to move to the folder you chose for your Java source code.

You can also do a java - version again to verify that Java is properly installed. Here I have opened my text editor. And before I enter any code, I will save the empty file to the same folder the command prompt window is using. Call this file FirstClass.java. Here is the first program we will work with.

You can pause this video and enter this program into your text editor. All programming languages have a set of rules. We typically call it syntax. This means you must enter the program exactly as you see it. Java is case-sensitive. This means that words such as public, class, void, and static must be entered exactly as shown. Otherwise, you will receive error messages when you try to run the program.

After saving the file, move to your command line window and enter java space FirstClass.java. If all has gone well, you should see the text “My First Class” in the command window as shown here. So what just happened?

The first action when you use the Java command is that your source code is compiled, or translated, into machine instructions called byte code that the virtual machine understands. Errors in your code syntax are caught during this phase, called compile time, and reported to you.

Only source code without syntax errors can be run. If no compile-time errors are detected, then the bytecode is executed in the virtual machine. Any errors that occur once the program begins execution are called runtime errors and usually result in the program ending and error messages displayed. This first program follows a pattern you will see in later examples.

Every Java program must have at least one file that contains a public class. A class, in turn, can contain data and methods. Methods are components of a class that carry out the work of a program. One method may be sufficient to carry out a program’s work, such as in our case, where the method perform displays text in the command prompt window.

In more complex solutions, the work is spread out amongst numerous methods, each responsible for one part of the solution. Every Java program must have at least one method called “main,” as you see in our program. This method is special in that when you run a Java program, it begins running in main.

In our first program, the first line in main takes our class, first class, and instantiates it. This means that the class will become an object in the computer’s memory. The variable firstClass is a reference to the object. With a reference, you can call upon methods defined in the class. This is what firstClass.perform() is doing.

The only work going on in this program is the display of the text between the quotation marks inside the parentheses that follows System.out.println. We call text inside of quotation marks a “string.” In this case, a literal string, because we can see the contents of this string in the program.

Before we move on, experiment by having the program display other messages. Try adding additional System.out.println statements. Next up, let’s take a look at data. The first thing you need to consider whenever you write code.