Printf in Java (Examples & Explanation)
One of the first commands in java you will learn is the print command. It has a few different variations and one of them is printf. I’ve been asked what is printf in java so many times and the simple explanation below answers the question all the time.
You will learn what is println in java and how to use it, then you will learn printf too and how you can use it.
Println is simple compared to printf, so a good understanding of the later is very important.
So let’s dive right in…
What is println in java?
Let’s start with a very common example:
‘System’ is that statement is a class, ‘out’ is an object and ‘println’ is a method. You don’t need to know the full meaning of all of that now, especially if you are just coming into the world of java programming. However, what you should notice are ‘println’ and the contents in the bracket, (“Hello World”).
What ‘println’ does is that it prints to the screen and makes the next item that will appear after that be on the next line.
So what the above statement will do is to display Hello World on the screen and make any other thing that will appear after that go under Hello World.
Hello World is a string and to print any string out to the screen, you have to put it inside double quotes “ “. See the following little program to understand the full meaning of what I am trying to say.
int a,b,c; a = 7; b =3; c = a+b; System.out.println(c);
I declared three variables a,b,and c then I gave a and b 7 and 3. Then I made c the addition of a and b. Now notice the last line of the code. I asked the program to print c to the screen. The value of c is 10 and will be printed to the screen.
Now compare this print command to the Hello World command. I didn’t enclose c inside quotes because it is a variable and not a string.
What if there is a need for you to print out both the string and the variable? For example, you want the output on the screen to be: The sum of the two numbers is 10
This is what the command will look like:
System.out.println(“The sum of the two numbers is “+ c);
Notice how the string and variable are joined using ‘+’ sign, and how each maintained the way it is supposed to be written i.e, the string part has its double quotes and the variable part stands on its own.
A similar example is when you want the value of variable a and b to appear on the screen. J You can quickly test yourself on how to do that before you look at the next line. L
System.out.println(“The sum of ”+a+ “ and ”+b+” is ”+c);
If you haven’t done this example before, I will suggest you take some time to practice it. I encourage that because practice is a key part of java programming.
Always keep in your mind how the strings are enclosed inside double quotes and variables stand on their own, and also, join the strings and variables with a plus sign.
Let’s head to another fun part… printf in java.
What is printf in java?
Is it even allowed in java?
That is a common question from people that can code using C++because they use it in that programming language.
Yes, printf is allowed in java.
Printf in java allows you to format how the results you want to print to screen should look. I’ll give you a nice example to illustrate the idea of printf soon. Now, know that printf simply stands for ‘print format’, a way to format what’s to be displayed.
System.out.prinln \\ prints and takes other outputs to the next line System.out.print \\ prints and leave other output on the same line Syste.out.printf \\ prints based on the FORMAT you give it. See the example below.
Printf in java example:
System.out.printf(“%3d” , a);
The above command will print ‘3 digits space’ before printing the value of variable ‘a’ to the screen on the same line.
With println, you will print to the screen then make all the other items appear just below what you printed.
Prinf, on the other hand, gives you total freedom on how to format what you print to the screen.