Printing on screen using print, println and printf
Let's talk about the ways to display messages on the screen, including:
System.out.println
System.out.print
System.out.printf
No, that's foolish ... from the moment you turn on your computer, even in their games and MSN messages are displayed on your screen.
They are very important and one of the most commonly used functions for digital devices.
Java: Printing on screen using print, println and printf
To start, go to your project 'First' and change the line:
System.out.println ("My first Java program");
By:
System.out.println ("My second Java program");
Besides the fact of no longer being the first but the second program that you created, what other differences have you noticed after compiling and running? It is able to figure out just by looking?
It is easy to see that the end of the sentence, the message "BUILD SUCCESSFUL (total time: 0 seconds) 'is no longer under the' My second Java program! ', but on the side.
'I know! After the sentence, I will hit enter!' It will look like this:
System.out.println ("My second Java program!"
+ "");
(This + "" appeared automatically)
Compiled, and ran ... made no difference.
Yes, hey. For Java, is the same thing. You can hit enter thousand times will remain the same.
The 'ln' from 'println' is 'line', because this function prints a line, and line includes a line break (or newline or \n, or [enter] or paragraph).
That is, the 'print' does not include this breakdown. How to put this break in 'print'?
There is a special symbol for this is the '\n'. Test here:
System.out.printl("My second Java program \n");
Now was not it? Ok!
So, as we would to appear on the screen:
Programming
Progressive
Thus:
System.out.println ("Program");
System.out.println ("Progressive");
Or so?
System.out.print ("Programming \n");
System.out.print ("Progressive \n");
Which of these codes is right? What is the best? Why use one over the other?
Here is a nice aspect of programming. Requires no right way to write code.
If the two forms have the same result, we can use both forms.
Will depend on your creativity.
It's quite common break your head, do a program of hundreds of thousands of lines, when comes someone and resolve within 20 or 30 lines.
Programming depends on your thinking, your way of thinking. It's something personal.
Check it out:
System.out.printf ("Progressive Development");
And then:
System.out.printf ("Progressive Program \ n");
And finally:
System.out.printf ("Progressive Development"
+ "");
Notice the difference? Calm down, do not troll you.
The 'f' of 'printf' is related to formatting, used to when you put numbers, strings (text), line up and do other types of formatting using the outputs.
We will see it better in other articles.
Then it's time to practice because you're able to solve the 10 exercises on outputs.
After solving, it can be considered the master in the arts to show messages on the screen.
Send your code, along with your name I format it and publish the post.
Click here to access the exercises.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment