When writing Java programs, you will often want to print out information to the user. This information is displayed in a text window known as the console.
Let’s start with an example program. Below is the code for a very simple Java program.
public class Hello
public static void main(String args)
What does this code do? Before we talk about printing, let’s review the first two lines.
The real action happens with the only statement in the program, repeated below:
This line of code prints out the text “Hello World” to the console. This text is called a string literal, which just means a set of characters that won’t change. We use quotes to show we want to print this exact phrase, but the quotes themselves are not printed. This helps the compiler tell that we’re not trying to use the name of a variable.
The print command has two versions. We have already seen how to simply print text. If we do this multiple times, however, it will put all of our text on a single line. It won’t even add spaces!
If we want to print text, then make sure the next phrase will be on a new line, we use a slightly different version of the print command: