How to Print Multiple Variables in Java

How to print multiple variables in java

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:

Output:

Country: India Capital: Delhi

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:

In case, you are just looking to print multiple variables in java directly without using any other text, you can do it as below:

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:

Output:

Country: India Capital: Delhi

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:

Output:

Country: India Capital: Delhi

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.

How to Print Multiple Integers in Java

You can print multiple integers with the help of System.out.print or System.out.printf.

Output:

1 2
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.

Output:

Employee 1
Employee 1

That’s all about how to print multiple variables in java.

Was this post helpful?

Related Posts

  • 24 September

    Increment for Loop by 2 in Java

    Table of ContentsHow to increment for loop by 2 in JavaHow to increment for loop by n in Java In this post, we will see how to increment for loop by 2 in Java. There are several looping statements available in Java and one of them is for loop in java. There are three parts […]

  • 20 September

    Return ArrayList in Java

    Table of ContentsReturn ArrayList in Java From a Static MethodReturn ArrayList in Java From a Non-static MethodConclusion This article discusses cases of returning an ArrayList in Java from a method. An ArrayList in Java is a collection of elements of the same data type under a single variable name. In different cases, you can return […]

  • Check if Object is null Java
    14 March

    Check if Object Is Null in Java

    Table of ContentsComparison Operator to Check if Object Is Null in JavaisNull() Method to Check if Object Is Null in JavanonNull() Method to Check if Object Is Null in JavarequireNonNull() Method to Check if Object Is Null in JavaConclusion An object in Java is an instance of a class. It is a real entity existing […]

  • What is == in java
    01 December

    What is == in java

    Table of ContentsWhat is meaning of == in JavaCode example of ==Object equality using equal to ==The difference between == and equals== in EnumConclusion In this tutorial, we will learn what is == in Java as it is a common lexeme that we will use while developing applications. The == lexeme can be confused with […]

  • Multiple classes in one file in Java
    18 October

    Multiple classes in one file in Java

    Table of ContentsIntroductionMethods to Implement Multiple Classes In One Java Program1) Nested classes2) Multiple non-static nested classes In this post, we will see how to have multiple classes in one file in java. Introduction You need to have any number of classes in a single Java file, but there is a restriction that you can […]

  • Break out of nested loop in java
    22 July

    How to break out of nested loops in Java

    Table of ContentsUsing break (will break inner loop)Using named loopUsing named blockUsing returnConclusion In this post, we will see how to break out of nested loops in Java. Using break (will break inner loop) It is very important to understand how nested loops work to ensure that applying break will output the desired result. If […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.