System.out.print – Printing outputs in Java


Keywords: Programming in Java, println, Java

Lär dig programmera Java på codebean.se!

Programming in Java is an excellent start if you want to learn to code. Java is platform-independent, object-oriented, simple and straightforward. Additionally, Java is one of the world’s most popular programming languages. This article guides you as a beginner to what programming in Java means.

Often you want to print a result – a call or an error message. The “System.out.print” method is a preprogrammed method in Java for printing text in the console.

How to print text and results in Java?

When programming, you often want to print a text. For example, it may be the answer to a calculation or an error message. Java has lots of built-in features to make it easier when coding, and print is one of them. This section takes a closer look at two ways to print text in Java

  • System.out.print () that prints text in the console
  • System.out.println () that prints text on a new row in the console

Additionally, we will also have a look at what you should be aware of when printing text in the Java console.

To print a text in the console window, use the pre-programmed method System.out.print.

 




System.out.print in Java

To print a text in the console window, use the pre-programmed method System.out.print. For example, if we want to print the text Hello World! we simply write

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

And get the result

Hello World!

Note the big letter in the word system and the semicolon at the end of the command. If you do not write the method in the right way (for example, ignore the upper and lower case), you will not be able to run the code. The compiler will find the error and print an error message.

 

System.out.println in Java – printing on a new line

If you want the text to be printed on a new line, you can use System.out.println(), and the text is printed on a new line. Notice the difference:

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

That gives the result

Hello World!Hello World!

or using:

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

that gives the result

Hello World!
Hello World!

That is, the System.out.print method prints the text on the same line, while the System.out.println method prints the text on a new line.

 

Combining multiple text strings in the same print

Furthermore, it is also possible to print multiple text strings simultaneously. We do this by adding a plus sign (+) between the two text strings we want to print, for example:

System.out.println("Today's date is: " + "2019-03-18");

Resulting in,

Today's date is: 2019-03-18

In other words, we print two text strings. The first text is “Today’s date is” and the second text string is “2019-03-18”. As you can see, the plus sign is not included in the printout. It is also possible to continue printing even more text strings by continuing with additional plus signs after each text string.

System.out.print in Java with mathematical operations

It is possible to print mathematical operations directly in the Java console. Similarly, we print the operation in the method. It is important not to forget when using quotes on what you want to print. Note the difference between these two operations

System.out.println("1 + 1");
System.out.println(1 + 1);

Resulting in:

1 + 1
2

In the second case, we have no quotes. This means that Java will interpret 1 + 1 as a mathematical operation instead of a text string. Then the answer to the operation will be printed, in other words,  the number 2. However, in the first case, Java interprets everything as a text string, since we have enclosed the text with quotation marks.

Try by yourself through onlinegdb.com.

 

Summary – System.out.print in Java

The two most common ways to print text in Java are with the methods System.out.print (prints text on the same line) and System.out.println (prints the text on a new line). Everything you enter inside the parentheses is printed. To summarize, print in Java:

  • The System.out.print method prints text on the same line in the console
  • The System.out.println method prints text on a new row in the console
  • You print multiple text strings simultaneously by inserting a plus sign (+) between the text strings
  • What you want to print should be quoted (” “)
  • You can perform mathematical operations directly in the printout