+ 1
How I can print two var in one println?
13 Answers
+ 2
int a=5,b=10;
System.out.println("var1="+a+" var2="+b);
output:
var1=5 var2=10
+ 2
With printf you can format what you want to print a bit easier than with println.
Example:
String firstName = "John";
String lastName = "Doe";
int age = 41;
System.out.println("Name: " + firstName + " " + lastName + "\n" + Age: " + age);
System.out.printf("Name: %s %s\nAge: %d\n", firstName, lastName, age);
As you can see, the second is a bit shorter and easier to read.
But the true power of printf is the formatting.
- %d is a format specifier for integer values.
In the above example is used for the age variable.
It tells to the printf method, that there should be an integer in the arguments. And it should be at the right place:
In the example we have two strings (the names), and then the age variable:
%s %s %d
=> the variables should be in the same order:
firstName, lastName, age
\n is escape character to go on the next line
But more about what the format specifiers can do, look here:
https://sharkysoft.com/archive/printf/docs/javadocs/lava/clib/stdio/doc-files/specification.htm
+ 1
in which language?
+ 1
in which language?
+ 1
if u asking about java.
int a=1;
int b=2;
System. out.println(a+" "+b);
+ 1
Yes I hope it will work @maryam
+ 1
int a = 5;
int b = 10;
System.out.println(a + " " + b); // 5 10
// You can use the print format method - printf
// %s - Strings, %d - Integers, %f - Float, \n - for new line
System.out.printf("%d %d\n", a, b); // 5 10
+ 1
Thank you so much
+ 1
%d, %f and %d are placeholders , google if for more info
+ 1
نعم وضحت الاجابة هي بمثابة تخصيص مكان للمتغيرات ...شكرا لكم جميعا
0
I will try it ^^
0
sal sal in jave
0
B G @ what is the different between printf and println and why you use % d