As we briefly hinted before, programs in Java are written in source code (which are plain text, human-readable files) that is processed by a compiler (in the case of Java, javac) to produce the Java bytecode in class files. The class files containing Java bytecode are, then, fed to a program called java, which contains the Java interpreter/JVM that executes the program that we wrote:
Syntax of a Simple Java Program
Like all programming languages, the source code in Java must follow particular syntaxes. Only then, will the program compile and provide accurate results. Since Java is an object-oriented programming language, everything in Java is enclosed within classes. A simple Java program looks similar to this:
Every java program file should have the same name as that of the class that contains main (). It is the entry point into the Java program.
Therefore the preceding program will compile and run without any errors only when these instructions are stored in a file called Test.java.
Another key feature of Java is that it is case-sensitive. This implies that System.out.Println will throw an error as it is not capitalized correctly. The correct instruction should be System.out.println.
main() should always be declared as shown in the sample. This is because, if main() is not a public method, it will not be accessed by the compiler, and the java program will not run. The reason main() is static is because we do not call it using any object, like you would for all other regular methods in Java.
Note
We will discuss these the public and static keywords later in this book, in greater depth.
Comments are used to provide some additional information. The Java compiler ignores these comments.
Single line comments are denoted by // and multiline comments are denoted by /* */.
Getting Input from the User
We previously studied a program that created output. Now, we are, going to study a complementary program: a program that gets input from the user so that the program can work based on what the user gives the program:
Now, we must dissect the structure of our new program, the one with the public class ReadInput. You might notice that it has more lines and that it is apparently more complex, but fret not: every single detail will be revealed (in all its full, glorious depth) when the time is right. But, for now, a simpler explanation will do, since we don't want to lose our focus on the principal, which is taking input from the user.
First, on line 1, we use the import keyword, which we have not seen yet. All Java code is organized in a hierarchical fashion, with many packages (we will discuss packages in more detail later, including how to make your own).
Here, hierarchy means "organized like a tree", similar to a family tree. In line 1 of the program, the word import simply means that we will use methods or classes that are organized in the java.io.Exception package.
On line 2, we, as before, create a new public class called ReadInput, without any surprises. As expected, the source code of this program will have to be inside a source file called ReadInput.java.
On line 3, we start the definition of our main method, but, this time, add a few words after the closing parentheses. The new words are throws IOException. Why is this needed?
The short explanation is: "Because, otherwise, the program will not compile." A longer version of the explanation is "Because when we read the input from the user, there may be an error and the Java language forces us to tell the compiler about some errors that our program may encounter during execution."
Also, line 3 is the line that's responsible for the need of the import in line 1: the IOException is a special class that is under the java.io.Exception hierarchy.
Line 5 is where the real action begins: we define a variable called inByte (short for "byte that will be input"), which will contain the results of the System.in.read method.
The System.in.read method, when executed, will take the first byte (and only one) from the standard input (usually, the keyboard, as we already discussed) and give it back as the answer to those who executed it (in this case, we, in line 5). We store this result in the inByte variable and continue the execution of the program.
With line 6, we print (to the standard output) a message saying what byte we read, using the standard way of calling the System.out.println method.
Notice that, for the sake of printing the byte (and not the internal number that represents the character for the computer), we had to use a construct of the following form:
An open parenthesis
The word char
A closing parenthesis
We use this before the variable named inByte. This construct is called a type cast and will be explained in much more detail in the lessons that follow.
On line 7, we use a different way to print the same message to the standard output. This is meant to show you how many tasks may be accomplished in more than one way and that there is "no single correct" way. Here, we use the System.out.println function.
The remaining lines simply close the braces of the main method definition and that of the ReadInput class.
Some of the main format strings for System.out.printf are listed in the following table:
There are many other formatting strings and many variables, and you can find the full specification on Oracle's website.
We will see some other common (modified) formatted strings, such as %.2f (which instructs the function to print a floating-point number with exactly two decimal digits after the decimal point, such as 2.57 or -123.45) and %03d (which instructs the function to print an integer with at least three places possibly left filled with 0s, such as 001 or 123 or 27204).