Java development software
In order to code and test Java programs, you need Java development software and must know how to use it. Before you go on, you MUST read and master the essential skill of running Java programs.
A sample Java program
The following program is a slight variation of the program that is part of the original Test project. When executed, it will display the message "Hello World!" on the system output device (the computer console):
/**
* This class defines a program that displays a message on the console.
*
* Written by: Jon Huhtala
*/
public class App {
// This required method is the starting point for processing.
public static void main(String[] args) {
System.out.println("Hello World!"); // Display the message.
}
}
Testing the sample program
Assuming you have a Test project and have already opened a Notepad window and a Command Prompt window, the procedure to test this sample program is as follows:
"Copy" the source statements (shown in blue above).
"Paste" the source statements into your Notepad window being sure to replace the entire contents of the window if it contained previous code.
Save the source file under the name "App.java" in your Test project.
Compile the "App.java" source file by entering the following command in the Command Prompt window:
javac App.java
Run the "App.class" bytecode file by entering the following command in the Command Prompt:
java App
Program analysis
Studying this sample program line-by-line will start you on the path to learning Java. The sections that follow will help you analyze the sample program. For now, it is not important that you understand everything completely. Through constant usage, you will eventually know what it all means.
Comments in Java
Are for documentation purposes only. The compiler ignores them when generating bytecode.
Three comment techniques are supported
Javadoc. Begin with /** and end with */. Everything between is treated as a comment.
Single-line. Begin with // and automatically continue to the end of the line.
Multi-line. Begin with /* and end with */. Everything between is treated as a comment.
Comments make programs easier to maintain. Good programmers document well enough that a person not familiar with the program can read the comments to know what each section of code is doing.
The program's class
public class App
is the class header. It declares that a class named App is to be defined and that it is to be publicly available for access from other places. In order for the Java Virtual Machine to find and use the program's class, always make it public. Other access modifiers (private, protected, and default or package access) will be presented later.
The words public and class are Java keywords and have special meaning. The class name is up to the programmer but must adhere to the restrictions on Java identifiers to be covered in the next lesson.
Statement blocks
Are modules of code consisting of one or more statements
Begin with an opening brace "{" and end with a closing brace "}"
May be entirely nested within another statement block. For example, the block for the main method is nested within the block for the App class in the sample program.
The main method
A required method (program module) in all Java programs except applets
The first method executed by the JVM when processing begins
In a small program, it defines application processing. In larger programs, it is the highest-level module and calls other methods to perform detailed processing.
Should have a method header coded as follows:
public static void main(String[] args)
This states that the method has public access, is a class (static) method not to be associated with any objects of the class, does not return a value to the caller (void), has the name main, and receives an array of string references (String[]) named args as parameters. It is not important that you understand all this now, just code the statement exactly as shown. The only flexibility you have in coding the header is the name of the array. The sample program will run just fine if you re-code the method header as
public static void main(String[] x)
Writing to the system output device (the console)
The system output device is already open and may be referenced by the identifier System.out where out is a public reference to a PrintStream defined within the System class. Again, it is not important that you understand all this now.
println is a public method of PrintStream objects that accepts a string parameter and copies it to the output device. In the sample program, the string is coded as a string literal enclosed within quotation marks.
Compiler requirements
All Java statements are delimited by a semicolon ";"
The compiler ignores "white space" characters (spaces, tabs, and the use of the return key). A good programmer, however, will use "white space" liberally to make their code easier to write, read, and maintain.
DO NOT write code like this
public class App {public static void main(String[] args){
System.out.println("Hello World!");}}even though it compiles and runs just fine. Try it and see!
Review questions
Which of the following are invalid headers for the main method (choose three)
public static void Main(String[] args)
public static void main(String[] parms)
public static void main(String args)
public static void main(string[] args)
public static void main(String[] args)
What will result from attempting to compile and execute the following?
public class App
{
public static void main(String[] parms)
{
// System.out.println("Java is fun");
}
}
it will not compile
it will compile but an error will occur at run time
it will compile and run but nothing will display
it will compile and run to display the message "Java is fun" on the console
What will result from attempting to compile and execute the following?
public class App {
public static void main(String[] parms) {
System.out.println("Java is fun");
}
}
it will not compile
it will compile but an error will occur at run time
it will compile and run but nothing will display
it will compile and run to display the message "Java is fun" on the console
What will result from attempting to compile and execute the following?
public class App
{
public static void main(String[] args)
{
System.out.println("Java is fun")
}
}
it will not compile
it will compile but an error will occur at run time
it will compile and run but nothing will display
it will compile and run to display the message "Java is fun" on the console