Chapter 2
Comments and Simple Printing
For documentation
Anything preceded by two forward slashes // (up to the end of a line) or enclosed between /* and */ (which can span multiple lines) is ignored by the compiler. These are comments-- to be read by you and other programmers. Typically, all methods (other than main) are preceded by a multi-line comment explaining the purpose of the method. Other lines of code that would benefit from a brief explanation are given single-line comments either on the previous line (i.e., above the code, not below it) or on the space remaining after the line.
/*
* This is a multiple-line comment.
* The extra asterisks at the beginning of these lines are not required,
* but they are helpful visually.
*/
public class First
{
public static void main(String[] args)
{
//Print a message (this is a single-line comment explaining the line of code below)
System.out.println("Hello, World!"); //this is another single-line comment
//System.out.println("This line won't run. It is commented out");
}
}
Note that there is third type of comment called a Javadoc comment that starts with /** (note the extra asterisk). Its purpose is to auto-generate documentation for code. This is not covered in this course, but I mention it because you will see if often in examples.
For debugging
Commenting can be beneficial in debugging (i.e., identifying errors in) code, as well. You can prevent specific lines of code from running (while testing your code) by putting // at their beginning; this is often referred to as “commenting out” the code. If you are not sure whether a particular line of code is causing a problem, you can comment it out to see if the code runs fine otherwise. For example, line 12 in the previous example is commented out and will not run (until the slashes are removed).
Printing
In computer programs, "printing" generally means displaying text on the console (the output window)-- not physically printing onto paper. There are two principal methods used for printing:
- System.out.println() prints whatever is enclosed in the (), then moves to the next line (so if you then print something else subsequently, it will appear on the line below).
- System.out.print() prints whatever is in the () and stays on the same line. Output from the next print() or println() statement will begin on that same line.
System.out.print("Hello, World!");
System.out.println(" It is I!"); //note the space at the beginning
System.out.println("I am on a new line.");
System.out.println(); //prints a blank line with no text
System.out.println("Goodbye");
Note that the code above is not a complete class. When providing examples, I will typically show only the part of the code that demonstrates a particular concept.
will output:
Hello, World! It is I!
I am on a new line.
Goodbye
You might be wondering what System.out refers to. System is a class in Java that is already written. You will see that we often use code from pre-existing classes, such as System, so we do not need to write everything ourselves from scratch.
The examples above involve printing text to the console. In programming, text is called a string (think of a string of characters). A string can contain any number of characters.