This time we'll talk about how to print text on the console screen with Java.
We already saw the System.out.println method in the "Hello World" program. That method is one of several ways to print something to the console screen, which we call the output system.
Beyond that method, other ways exist. In general there are 3 methods for showing output in Java:
System.out.print: prints without moving to a new lineSystem.out.println: prints and moves to a new lineSystem.out.printf: prints with a specific format
Let's go deeper..
The Output System with System.out.print and System.out.println
Since they're similar, we'll cover these 2 methods together.
Both System.out.print and System.out.println do the same job, printing whatever you pass as an argument to the console screen or standard output.
The difference is that System.out.print prints to the right, while System.out.println prints downward (new line / print line).
To make it clear, let's just code.
public class SistemOutput{
public static void main(String[] args) {
// first 4 output lines use println
System.out.println("Dengan System.out.println: ");
System.out.println("satu,");
System.out.println("dua,");
System.out.println("tiga.");
// the next lines use print
System.out.print("Dengan System.out.print: ");
System.out.print("empat,");
System.out.print("lima,");
System.out.print("enam.");
}
}
Now let's run it, and the result will look more or less like this:
Dengan System.out.println:
satu,
dua,
tiga.
Dengan System.out.print: empat,lima,enam.
You can see the difference, right.
With println, the words satu, dua, and tiga sit on separate lines. With print, the words empat, lima, and enam sit on the same line.
Both the print and println methods can print literals, expressions, and variables.
We'll cover literals and variables more in the next piece. For now let's just look at an example.
public class SistemOutput{
public static void main(String[] args) {
// print a string literal
System.out.println("ini string literal");
// print an integer literal
System.out.println(24);
// print the contents of a variable
String nama = "Inva";
System.out.println(nama);
// print an expression (a data operation)
System.out.println("2 + 8 adalah " + (2+8));
}
}
If you run it, the output looks like this:
ini string literal
24
Inva
2 + 8 adalah 10
The Output System with System.out.printf
If the 2 previous methods print whatever you pass as an argument, printf is a little different.
System.out.printf works by taking a format or template as its first argument. That format is then filled with the values in the following arguments.
That format or template is an ordinary string literal. The only difference is a few special characters that act as placeholders, marked with a % sign followed by a number and letter as needed.
Let's look at a code example.
public class SistemOutput{
public static void main(String[] args) {
// %s will be replaced with "Budi"
// %d will be replaced with 12
System.out.printf("Nama saya %s saya kelas %d\n", "Budi", 12);
String nama = "Adi";
String kegiatan = "belajar Java";
// the first %s is replaced with the contents of the nama variable
// the second %s is replaced with the contents of the kegiatan variable
System.out.printf("Saya %s sedang %s", nama, kegiatan);
}
}
\nin that code is an escape character new line, and its job is to print a new line. There's also\tfor printing atabcharacter. More on escape character can be read on wikipedia.
If the program runs, the output looks like this:
Nama saya Budi saya kelas 12
Saya Adi sedang belajar Java
In that example we use 2 format strings, %s and %d. Here %s shows a literal of the character-set type (String), while %d shows a number literal (int).
String Format Placeholders in Java
Besides the 2 formats in the code above, there are other formats you can use.
%s: for a character set (String)%d: for whole numbers (intandlong)%f: for decimal numbers (floatanddouble)%c: for a unicode character (char)%t: for a time format (Date/Time)
Controlling Format String Width
You can also control the width or padding of the format string you build. The trick is to put a number right after the % sign.
A positive number means right-aligned, while a negative number means left-aligned. Let's try coding.
public class SistemOutput{
public static void main(String[] args) {
System.out.println("Dengan println:");
System.out.println("Nama : Adi");
System.out.println("Umur : 19 tahun");
System.out.println("Kegiatan : Belajar Java");
System.out.println(); // empty println to create a blank line
System.out.println("Dengan printf angka positif:");
System.out.printf("%8s : %s\n", "Nama", "Adi");
System.out.printf("%8s : %d tahun\n", "Umur", 19);
System.out.printf("%8s : %s\n", "Kegiatan", "Belajar Java");
System.out.println();
System.out.println("Dengan printf angka negatif:");
System.out.printf("%-8s : %s\n", "Nama", "Adi");
System.out.printf("%-8s : %d tahun\n", "Umur", 19);
System.out.printf("%-8s : %s\n", "Kegiatan", "Belajar Java");
}
}
If you run it, the output looks like this:
Dengan println:
Nama : Adi
Umur : 19 tahun
Kegiatan : Belajar Java
Dengan printf angka positif:
Nama : Adi
Umur : 19 tahun
Kegiatan : Belajar Java
Dengan printf angka negatif:
Nama : Adi
Umur : 19 tahun
Kegiatan : Belajar Java
You can see the result. The number 8 was chosen because the longest word, "kegiatan", has 8 characters. The output shows the ":" signs lining up without adding spaces by hand.
Besides positive and negative whole numbers, you can also use decimal numbers. A decimal number does different things for different formats.
For the %s format, the number after the point limits how many characters are shown. For the %f format, the number after the point sets the precision you want.
Let's look at an example
public class SistemOutput{
public static void main(String[] args) {
System.out.printf("| %5s |\n", "Senin");
System.out.printf("| %5.3s |\n", "Senin"); // Shows Sen right-aligned, width 5
System.out.printf("| %-5.3s |\n", "Senin"); // Shows Sen left-aligned, width 5
System.out.printf("| %.3s |\n", "Senin"); // Shows Sen
System.out.println();
System.out.printf("PI = %f\n", 3.141592);
System.out.printf("PI = %.2f\n", 3.141592); // Shows 3.14
System.out.printf("PI = %8.2f\n", 3.141592); // Shows 3.14 right-aligned, width 8
}
}
If we run it, the result looks like this:
| Senin |
| Sen |
| Sen |
| Sen |
PI = 3.141592
PI = 3.14
PI = 3.14
Next Up
This output system is worth remembering, because you'll use it in every bit of code. So don't forget it — if you do, you can always come back here hehe.
Next time we'll start playing with data. We'll cover variables and data types in the Java language.
Happy coding 👋