How to print something on the screen

The word “print” does not mean the printing using a tangible printer. We use that word to tell that we are displaying something on the screen.

It is easy in JAVA.

System.out.print(“Hello Java”);

or

System.out.println(“Hello Java”);

Let’s identify the important features of the two statements given above.

  1. The “S” in the word “System” should be capital.
  2. Whatever given inside the brackets will be printed.
  3. If you want to print a string, you need to use inverted commas.
  4. If you want to print a predefined variable, just use the variable name.
  5. Use “print”(first statement) to keep the cursor in the same line.
  6. Use “println”(second statement) to move the cursor to the next line.

Important :

Whatever you type in java, you need to type inside the main class.

Capture

More zoomed picture is given below.

Capture1

Can you understand what happened?

First “Hello Java” is printed. Since we used just the “print”, the cursor is not moved down. Then again “Hello Java” is printed without a space even. That is why you get “JavaHello” word in the middle.

Let’s print it in a polite manner.

Capture2

Likewise you can print a predefined variable also.

3

I made 2 variables , an integer and a string.

Then I printed them.

Now we’ll learn how to concatenate inside a print statement.

System.out.println(“I am  “+ age + ” years old. \n My school is ” + school);

 

Concatenation in JAVA is done by the plus (+) sign.

The “\n” takes the cursor to the next line. So the cursor will be moved to the next line in the middle of the print statement.

Just look at the output now.

4

So that’s it for this section. See you soon in the next section.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s