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:
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:
For printing a variable’s value:
For printing variables with some text as output:
Example
Here, we have two integer type variables, “a” and “b”. We will print these variables using the “System.out.print()” method:
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:
For instance, suppose you want to display the following text as output:
For printing only variable:
For printing variables with some text as output:
Example
We will now print “a” and “b” variables by using the “System.out.println()” method:
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:
Here, you have to add a “specifier” and variable name as a “parameter”.
For example:
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:
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.