1. Getting to know the System.out.println command
You have just written your first program in Java and printed the first message to the screen. Congratulations on that. Let’s not stop now…
Any Java program consists of commands. Commands come in different kinds. The Java language has commands for every occasion. Each command describes a specific action. At the end of each command, a semicolon is placed.
Examples of commands:
| Command | Description (what it does) |
|---|---|
|
Prints the number to the screen: |
|
Prints the text: |
|
Prints the text: |
In fact, this is a single command — System.out.println. And in parentheses you pass parameters to it. Depending on the parameter values, one command can perform different actions. That’s very convenient.
Case matters
In Java, case matters: it’s important whether commands are written with uppercase or lowercase letters. The command System.out.println() will work, but system.out.println() will not. If you write a command with the wrong case, Java won’t recognize it and therefore won’t execute it. So be careful.
Double quotes
If you want to print text to the screen, you need to enclose it in double quotes.
A single quote looks like this ', and a double quote looks like this ". A double quote is not two single quotes—please don’t confuse them.
The double quote is the one next to the Enter key. It often also has the Cyrillic letter "E" on it.
2. Getting to know the System.out.print command
There is another command similar to System.out.println(), and it is called System.out.print().
The System.out.println() command prints text to the screen and then adds a line break, as if pressing Enter. This way, the next System.out.println() command will print text on the next line. And the next command—on the next line, and so on.
Unlike System.out.println(), the System.out.print() command does not add Enter after printing text. Therefore, text printed by several System.out.print() commands will be printed on a single line.
Example 1:System.out.print("Amigo");
System.out.println("The");
System.out.print("Best");
This will print:
AmigoThe
Best
System.out.print("Amigo");
System.out.print("The");
System.out.print("Best");
This will print:
AmigoTheBest
If you want to print text on one line, use print(); if on multiple lines—use println(). It’s that simple.
3. Breaking down the System.out.println command
If you know at least a little English, learning programming will be much easier. The names of many commands in Java are directly related to what they do.
For example, println is print + line — print line. The word print is familiar to everyone, and why specifically “line,” I’ll explain now. Each line of text on the screen is called a line. Therefore, to write a line means to output a line of text.
System.out is a special system object that prints text to the screen. It’s also called the console. The console is one of the most common ways for a program and a person to interact. When a user types commands to a program on the keyboard, and it responds by printing text to the screen, programmers say that the program works with the console.
In other words, working with the console is printing text to the screen and inputting data using the keyboard. So if you hear in the future that a program printed something to the console, that simply means it printed text to the screen.
So it turns out that System.out.println is essentially “Hey, console, print me a line of text.”
4. Examples of printing various data
Let’s play around with console output. We’ll start with the simplest and gradually make it more complex:
Printing numbers
System.out.println(1001);
System.out.println(-15);
Printing text
System.out.println("Programming is awesome!");
System.out.println("Just kidding — now let’s code.");
Printing multiple values at once
Trick: you can combine strings using +. Although it’s better to call it concatenation:
System.out.println("My age: " + 28);
This will print:
My age: 28
You can also concatenate several values in a row:
System.out.println("The year is " + 2025 + ".");
This will print:
The year is 2025.
5. Common mistakes when using System.out.println
Young Jedi often run into the same pitfalls. So that you don’t become the hero of the book “1001 Mistakes in Java,” here’s a list of common gotchas and tips on how to avoid them.
Mistake 1. Forgot to close the string—missing the second quote:
System.out.println("Oops, opened a quote and forgot to close it);
The compiler will complain: "unclosed string literal" — close your strings!
Mistake 2. Missing semicolon
System.out.println("No ; at the end")
But Java is unforgiving. Fix it—add a semicolon at the end of the line.
Mistake 3. Using the wrong quotes
System.out.println(‘Error’); // wrong!
System.out.println('Error'); // also wrong!
For strings, always use standard double quotes—like these: ".
GO TO FULL VERSION
s