Table of Contents
In this post, we will see how to print multiple variables in java.
Ways to Print Multiple Variables in Java
Here are two primary ways to print multiple variables in java.
Using System.out.print
You can use System.out.println to print two String variables in java as below:
1 2 3 4 5 6 7 8 9 10 11 12 |
package org.arpit.java2blog; public class PrintMultipleVariablesMain { public static void main(String[] args) { String countryInd = "India"; String capitalDel = "Delhi"; System.out.println("Country: "+countryInd+" Capital: "+capitalDel); } } |
Output:
As you can see, we have simply used System.out.println to print two variables on the console. You can use it to print multiple variables as well. Let’s say you have 3rd variables as str3
, then you can print it as below:
1 2 3 |
System.out.print("Country: "+countryInd+" Capital: "+capitalDel+" Variable 3: "+str3); |
In case, you are just looking to print multiple variables in java directly without using any other text, you can do it as below:
1 2 3 |
System.out.print(str1 +" "+ str2+" "+str3); |
Using System.out.printf
You can also use System.out.printf
to print multiple variables in java.
Let’s see with the help of simple example:
1 2 3 4 5 6 7 8 9 10 11 12 |
package org.arpit.java2blog; public class PrintMultipleVariablesMain { public static void main(String[] args) { String countryInd = "India"; String capitalDel = "Delhi"; System.out.printf("Country: %s Capital: %s",countryInd,capitalDel); } } |
Output:
Print Multiple Variables in Java Using Logger
In case, you are using logger, then you can use log.info()
to print multiple variables. Obviously, you can choose logging level based on your preference.
Here is an example:
1 2 3 4 5 6 7 8 9 10 11 12 |
package org.arpit.java2blog; public class PrintMultipleVariablesMain { public static void main(String[] args) { String countryInd = "India"; String capitalDel = "Delhi"; log.info("Country: {} Capital: {}",countryInd,capitalDel) } } |
Output:
As you can see, we are using curtly braces({}
) to put placeholder here.
Frequently Asked Questions
How Do I Print Multiple Values on One Line in Java
You can use System.out.print
or System.out.printf
to print multiple values on one line in java.
1 2 3 4 |
System.out.print(str1 +" "+ str2); System.out.printf("%s %s",str1,str2); |
How to Print Multiple Integers in Java
You can print multiple integers with the help of System.out.print
or System.out.printf
.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package org.arpit.java2blog; public class PrintMultipleVariablesMain { public static void main(String[] args) { int i=1; int j=2; System.out.println(i+" "+j); System.out.printf("%d %d",i,j); } } |
Output:
1 2
How to Print String and Integer in Same Line in Java
Here is example to print string and integer in same line in java.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package org.arpit.java2blog; public class PrintMultipleVariablesMain { public static void main(String[] args) { String str1="Employee"; int i=1; System.out.println(str1+" "+i); System.out.printf("%s %d",str1,i); } } |
Output:
Employee 1
That’s all about how to print multiple variables in java.