Java

How to Print a Variable in Java?

A variable is a storage unit where the value is stored while executing the program. In Java, we can print the value of a variable by utilizing the System class methods. The System class is the predefined Java class, and it provides standard input, standard output, and error output streams, among other features. It is also extended from the Java Object class.

This blog will demonstrate the methods for printing a variable in Java.

How to Print a Variable in Java?

To print a variable in Java, you can use the following methods of the System class:

  • out.print()
  • out.println()
  • out.printf()

We will now check out each of the mentioned methods one by one!

Method 1: Printing a Variable in Java Using System.out.print() Method

The print() method prints any statement, variable, or both with the ‘+’ operator. It prints the value in the same line, and the cursor has moved to the end of the output.

The “System.out.print()” method is utilized to print any statement, the value of a variable, or both with the help of the concatenation operator “+”. It prints the value in a single line and moves the cursor to the end of the output.

Syntax
The syntax of the “System.out.print()” method is:

System.out.print(argument);

Here, passing an argument is necessary; if you do not pass the argument, the “System.out.print()” method will throw an error.

For example, print some text as output:

System.out.print("Text");

For printing a variable’s value:

System.out.print(a);

For printing variables with some text as output:

System.out.print("Text" + a);

Example
Here, we have two integer type variables, “a” and “b”. We will print these variables using the “System.out.print()” method:

int a = 5;
int b = 10;
System.out.print("Value of variable 'a' is " + a);
System.out.print(" Value of variable 'b' is " + b);

The output of the given two print statements will be in the same line because the cursor is located at the end of the first printed statement and place the second one in front of it:

Want to print each variable in separate lines? Follow the below-given section.

Method 2: Printing a Variable in Java Using System.out.println() Method

The “System.out.println()” method is the enhanced form of the “print()” method. It is utilized to print the given values or variables in separate lines. In the “println()” method, the passing argument is not mandatory; if it is empty, the cursor will move to the next line rather than throwing an error.

Syntax
The syntax of the “System.out.println()” method is:

System.out.println();

For instance, suppose you want to display the following text as output:

System.out.println("Text");

For printing only variable:

System.out.println(a);

For printing variables with some text as output:

System.out.println("Text" + a);

Example
We will now print “a” and “b” variables by using the “System.out.println()” method:

System.out.println("Value of variable 'a' is "+ a);
System.out.print("Value of variable 'b' is "+ b);

It will show the output of every single print statement starting from the next line:

Method 3: Printing a Variable in Java Using System.out.printf() Method

There is another method, “System.out.printf()” used to print the variable in a Java program. This method needs a format specifier while calling, such as %f, %d, and so on. These specifiers are used to specify the format of the variables. For instance, “%f” indicates the “Decimal floating-point” while “%d” represents the “Decimal integer”.

Syntax
The syntax of the “System.out.printf()” method is:

System.out.printf(format specifier,parameter);

Here, you have to add a “specifier” and variable name as a “parameter”.

For example:

System.out.print("%d Text", a);

The output will print first the format specifier’s value and then “Text”.

Example
We will now use the “printf()” method to print out the value of the “a” variable. Here, we will use “%d” as the format specifier because our variable is in integer type:

int a = 5;
System.out.printf("%d is a value of variable 'a'", a);

Output

We compiled all basic information related to printing variables in Java.

Conclusion

To print the variable in Java, there are three print methods of the System class: print(), println(), and printf() methods. The print() method prints out all statements in a single line. Whereas the println() method prints the value of the added variables or text in separate lines. In contrast, the printf() method utilizes a format specifier according to the types of the specified variable. This blog demonstrated the methods on how to print a variable in Java.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.