2. Comments and Errors
Comments
Every single character in the previous example was an important part of the program. However, you can add text that does not run in two ways:
//This is a single line comment.
public class MyProgram { //The comment starts after two slashes
/**
* This is a multi-line comment. The slash star begins the comment
* The comment ends with a star slash.
*
* Sometimes people write comments with many stars in them
* Only the slash star at the beginning and the star slash
* at the end affect where the comment starts and ends.
* It is important not to have a space between the / and the *
*/
public static void main(String[] args) {
System.out.println("Hello World!"); //Outputs "Hello World"
System.out.println("Hello Moon"); //Outputs "Hello Moon"
System.out.println("Hello Mars"); //Notice that lines run sequentially
}
}
In most code editors, you can automatically toggle commenting of the selected line(s) by holding the control key and the forward slash "/" key.
Errors
There are three kinds of errors that can occur in your program:
- Compilation errors occur when the computer cannot understand your application, and Java cannot compile the text into a runnable executable. The program will not start until you fix the "syntax error". Java has syntax and grammar similar to the C and C++ programming languages. Unlike humans, computers will often fail at understanding a program even if one tiny syntax error is present.
- Runtime errors occur when the program starts, but hits a problem during execution. For example, you may try to divide a number by zero, open a file that does not exist, or use too much memory.
- Logic errors occur when the program runs successfully, but it does not perform what the programmer wanted it to. These are some of the hardest errors ("bugs") to fix.
Understanding the hello world program
Your first Java program executes one statement of code at a time. A statement is usually a line of code that ends in a semicolon ";", but we will see that not all statements are like this.
What does public class Main and public static void main(String[] args) mean?
Java is an Object Oriented Language (OOP). Unfortunately, we will not fully understand these lines until much later in the book. For now, you should know that:
- The file name must be the same as the public class
- The
public static void main(String[] args){
is always the starting point of your program
If you have programmed before, you should know that this class merely serves as the container for the main method. The public
means that the method can be executed from any class. The static
means that the method can be executed without an associate instance class object. The void
means the method does not return any value. main
is the method name, which cannot be changed for a starting function. The String[] args
are the command line parameters that are passed into the application when it starts
Try adding comments and see that the program output doesn't change. Also try removing the semicolon to see a compilation error:
2. Comments and Errors
Comments
Every single character in the previous example was an important part of the program. However, you can add text that does not run in two ways:
In most code editors, you can automatically toggle commenting of the selected line(s) by holding the control key and the forward slash "/" key.
Errors
There are three kinds of errors that can occur in your program:
Understanding the hello world program
Your first Java program executes one statement of code at a time. A statement is usually a line of code that ends in a semicolon ";", but we will see that not all statements are like this.
What does public class Main and public static void main(String[] args) mean?
Java is an Object Oriented Language (OOP). Unfortunately, we will not fully understand these lines until much later in the book. For now, you should know that:
public static void main(String[] args){
is always the starting point of your programIf you have programmed before, you should know that this class merely serves as the container for the main method. The
public
means that the method can be executed from any class. Thestatic
means that the method can be executed without an associate instance class object. Thevoid
means the method does not return any value.main
is the method name, which cannot be changed for a starting function. TheString[] args
are the command line parameters that are passed into the application when it startsTry adding comments and see that the program output doesn't change. Also try removing the semicolon to see a compilation error: