Displaying Data | Heaton Research

Displaying Data

Get the entire book!
Java for the Beginning Programmer

We have seen that there are two different ways to display data to the console window. The following two methods both display data to the console window.

System.out.print
System.out.println

But what exactly is the difference between these two methods? First, we will look at what System.out.println does. Consider the following two lines of code.

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

These two lines would produce the following output.

Hello
World

Now consider if we were to use print instead of println. Consider the following two lines of code that use print.

System.out.print("Hello");
System.out.print("World");

If you executed the above two lines of code you would get a different output. These two lines of code would produce the following output.

HelloWorld

As you can see, System.out.println moves to a new line. Whereas System.out.print does not move to a new line.

Copyright 2005-2008 by Heaton Research, Inc.