While
println will take a String, print it and add a new line at the end,
printf will take a
formatted String and not add a new line at the end. A formatted String means, that you can do stuff like this:
- for(int i=1; i<=10; i++) {
-
System.out.printf("The given number is %d and it is %s\n", i, (i % 2 == 0 ? "even" : "odd"));
-
}
This will print
- The given number is 1 and it is odd.
-
The given number is 2 and it is even.
-
The given number is 3 and it is odd.
-
The given number is 4 and it is even.
-
The given number is 5 and it is odd.
-
The given number is 6 and it is even.
-
The given number is 7 and it is odd.
-
The given number is 8 and it is even.
-
The given number is 9 and it is odd.
-
The given number is 10 and it is even.
-
So in short:
%s is used when you want to have a String at that place,
%d is used for integers.