How to display messages in the console?

Facebooktwittergoogle_plustumblrFacebooktwittergoogle_plustumblr

Computer-Vertex-Academy

There are two methods to display messages in the console: System.out.println () and System.out.print (). What’s the difference?

Differences between System.out.println () and System.out.print ():

  • out.println displays a message on the screen and moves the cursor to the new line
  • out.print () displays a message on the screen but it doesn’t move the cursor to the new line

Let's look at couple examples for better understanding.

Example 1

If you try to run this code on your PC, you will see the following in the console:

I learn

Java

Comments:

We used System.out.println (). That’s why the cursor automatically moved to the new line after the phrase “I learn” was displayed at the screen, just as if we pressed enter.

Remember: System.out.println () displays the message at the screen and then moves the cursor to the new line.


Example 2

If you try to run this code on your PC, you will see the following in the console:

I learn

Java

Cool!

Comments:

After displaying “I learn” the cursor was automatically moved to the new line.

It displayed “Java” and the cursor was moved to the new line as well.

It displayed “Cool!”, then the cursor was automatically moved to the new line.

Remember: System.out.println () displays the message at the screen and then moves the cursor to the new line.


Example 3

If you try to run this code on your PC, you will see the following in the console:

I learnJava

Comments:

Since in this program we used

the cursor wasn’t moved to the new line after displaying “I learn”. That’s why the word “Java” is displayed straight after “I learn”.

But how do I add a space between “I learn” and “Java”?

That is how:

Do you notice the difference from the previous version of the code?

Yes, we added a space. And now if you run this code on your PC, you will see the following in the console:

I learn Java


LET’S SUMMARIZE:

System.out.println () moves the cursor to the new line

System.out.print ()  doesn’t move the cursor to the new line

P.S. „Println” in System.out.println () stands for „printnewline“.

Facebooktwittergoogle_plustumblrFacebooktwittergoogle_plustumblr

Facebooktwittergoogle_plustumblrFacebooktwittergoogle_plustumblr

Publiction author

offline 4 hours

Vertex Academy

Comments: 0Publics: 274Registration: 06-03-2016

Leave a Reply

Your email address will not be published.

Subscribe news

Subscribe to our newsletter we will keep you up-to-date!

Самоучители--узнать детальнее--