JAVA Fundamentals | Part 3 | Learning to print text on the monitor

Leave a Comment
Write Once, Run Anywhere!
"Write Once, Run Anywhere!"


Hey Folks!

Continuing with our tutorials on JAVA, today we'll learn how to use some common functions to print text on the monitor screen.

Like you'd remember from the Hello World example we saw in the last article, you may remember we used the following code to display "Hello World!" on the screen:
  • System.out.println("Hello World!");
We also discussed that in this statement above,
System is a class
out is an output stream object
println is a method
Hello World! is a string that the println method receives as an argument.

Hence continuing the same discussion, let us understand the println method and also various other commonly used methods to display and manipulate text to be shown on screen in JAVA.



With a statement like
  • System.out.println("Hello World!");
the method prints Hello World! on the screen and then moves the cursor to the next line. The argument received must be a string.

Another alternative to the println method is a print method. A respective statement using print method would look like this:
  • System.out.print("Hello World!");
the method prints Hello World! on the screen. It DOES NOT move the cursor to the next line. The argument received must be a string.

A simple code example to illustrate the difference between println and print is shown as below:

Example illustrating the difference between println and print methods I
Example illustrating the difference between println and print methods I
The output that the above code produces is:

Example illustrating the difference between println and print methods II
Example illustrating the difference between println and print methods II
Here as we can see, in line no. 6 where println was used, the cursor was shifted to the next line immediately after its execution. Hence the print statements at line no. 7,8 and 9 executed in the line next to it and the usage of print method didn't allow the shifting of cursor position to the next line after its execution.

Now we can also use escape sequences to perform text manipulations in JAVA.
An escape sequence is a character combination consisting of a backslash (\) followed by a letter or by a combination of digits.
For example the escape sequence \n changes the cursor position to the beginning of the next line with respect to the line the cursor is in currently.
Using \t will result in a tab space.

We can see the use of \n in the following code snippet.

Input
Input
Output
Output

Also there is one more method we can use to display text in JAVA. Its called the printf function.
The printf function uses some predefined formats.
For example the format for a string is %s
format for a new line character is %n
format for an integer is %d
and so on.

So we can use the printf function also to produce the same output as we did produce above as shown in the code snippet below.

Input
Input
Output
Output

Notice the formats used for strings and new line characters. 
Notice the respective order they are used in. 
Also notice that printf can function like both println and print functions according to if a %n is placed at the end or not.

Finally, there are hundreds of similar methods in JAVA for printing different data. I'd highly recommend you to visit the following link and look for the various "print" methods in JAVA for yourself except for the 3 we have discussed here.

http://docs.oracle.com/javase/8/docs/api/java/io/PrintStream.html

I hope this will give you a basic overview of text representation using JAVA. Stay tuned as we advance deeper into this language.

Take care :)



0 comments:

Post a Comment

Powered by Blogger.