Keywords: Programming in Java, println, Java
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.
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
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.
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
And get the result
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.
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!
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.
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");
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.
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);
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.
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: