As we know that java is made by the C programming. The java has many common features to C language. While we wants to print in java we usually use the following code.
1 2 3 |
System.out.println(<em>statement1</em>); or System.out.print(<em>statement2</em>); |
But there is also the feature of printf in java which we used to do in C programming:
Syntax:
1 2 3 4 5 |
//In C language <strong>printf</strong>("format_string", comma_separated_list_of_expressions); //In JAVA System.out.<strong>printf</strong>("format_string", separated_list_of_expressions); |
For eg:
1 2 3 4 5 6 |
//In C we use void main(){ int sum = 4; printf("The sum is %d , sum); } |
which outputs like this in C :
But in Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
//In JAVA we use public class PrintJava { int sum = 4; public void display(){ System.out.printf("The value of sum is %d", sum); } public static void main (String[] args) { PrintJava test = new PrintJava(); test.display(); } } |
which outputs like this in JAVA :
Some of the more commonly used conversion symbols in java Print Stream printf() are as follows:
I want to say – thank you for this!