Java: Output


This Java video gives a simple explanation of the "Hello World!" program that we wrote in Java Lesson 1, along with some additional examples of formatting printed output. The original "Hello World!" program is shown below.

Hello World! Program

We are going to start the future Java lessons as we started this one, with a project like the one we created in Java Lesson 1. We will then go on to add code exactly where we did in lesson 1, after the line

// TODO code application logic here

In Java Lesson 1, we added this single line of text:

System.out.println("Hello World!");

The first half of this line contains three parts that are separated by commas: System, out, and println. The word "System" refers to a part of the Java standard. The word "out" refers to the fact that we are performing output. Lastly, the command "println" is short for "print line." So, this entire command prints a line of text to the stardard output, which in NetBeans is the pane that we saw in lesson 1 (shown below).

Hello World! Output

Immediately after those three parts of the command in our program, we have the text in parentheses and quotation marks. The parentheses form part of the printing command, and the quotation marks and text form what is refered to as the string literal: "Hello World!".

For our second program, we print three separate lines of text: XoaX.net, Java, Lesson 1. By using the command System.out.println three times, we created three lines of text with three separate string literals: "XoaX.net", "Java", "Lesson 1".

Program Java Lesson 2

The output of this second program is shown below.

Java Lesson 2 Output

We can put all of this text on one line by replacing the println with a print as we show below. For clarity and visibility, we added spaces at the end of the first two string literals.

Using Print in Java Programs

As we can see from the output below, all of the text is on one line now. Notice that we kept the last println as is because it ends the line of text.

Using Print Output

Finally, we remark that we can add line spaces to our output by using println with no string literal inside the parentheses. Here, we have taken the original program and added line spaces between each of the lines.

Adding Line Spaces in Java

Below, we show the output with the blank lines in it.

Line Spaces Output