Print Variables in Java
-
Print Variable Using the
print()
Function in Java -
Print Variable Using the
println()
Function in Java -
Print Variable Using the
printf()
Function - Conclusion

We can store data in a computer program using a variable. A variable is a name of a memory region that holds the value while we execute the Java program.
Also, each variable has a data type assigned to it. This article will discuss different ways to print variables in Java.
If we are given a variable in Java, we can print it by using the print()
method, the println()
method, and the printf()
method. Let’s go and take a closer look at how they work.
Print Variable Using the print()
Function in Java
We can print variables using the print()
method. Let us see the syntax for the print()
method.
Syntax:
System.out.print(parameter)
As we can see from the syntax, we can break the statement into three parts.
The first element is the System
, a final class specified in java.lang
package. Then we have the second element as out
, public, and static member field of the System
class.
The last element that we have is the print()
method, which is a public method that we can use on the out
member field. After printing the output, we have the cursor at the last point of the output.
Let us have a look at the code below to understand how it works.
public class Main
{
public static void main(String args[])
{
int x=10;
int y=20;
int res=x+y;
System.out.print(res);
}
}
Output:
30
We need to keep one point in mind while working with the print()
method. We must always pass arguments in this method. Otherwise, it will result in an error with the message no suitable method found for print(no arguments)
.
Let us look at the code below to get a better understanding.
public class Main
{
public static void main(String args[])
{
System.out.print();
}
}
Output:
/Main.java:5: error: no suitable method found for print(no arguments)
System.out.print();
^
method PrintStream.print(boolean) is not applicable
(actual and formal argument lists differ in length)
method PrintStream.print(char) is not applicable
(actual and formal argument lists differ in length)
method PrintStream.print(int) is not applicable
(actual and formal argument lists differ in length)
method PrintStream.print(long) is not applicable
(actual and formal argument lists differ in length)
method PrintStream.print(float) is not applicable
(actual and formal argument lists differ in length)
method PrintStream.print(double) is not applicable
(actual and formal argument lists differ in length)
method PrintStream.print(char[]) is not applicable
(actual and formal argument lists differ in length)
method PrintStream.print(String) is not applicable
(actual and formal argument lists differ in length)
method PrintStream.print(Object) is not applicable
(actual and formal argument lists differ in length)
1 error
Print Variable Using the println()
Function in Java
We can also use System.out.println()
to print a variable in Java. Let us see the syntax for the println()
method.
Syntax:
System.out.println(parameter)
As we can see from the syntax, the println()
method is almost the same as the print()
method. The only difference is that when we utilize the println()
method, the cursor moves to the next line after printing the output.
Let us have a look at the code below to understand how it works.
public class Main
{
public static void main(String args[])
{
int x=10;
int y=20;
int res=x+y;
System.out.println(res);
}
}
Output:
30
When working with the println()
method, we will not get an error even if we don’t pass an argument. Let us look at the code below to get a better understanding.
public class Main
{
public static void main(String args[])
{
System.out.println();
}
}
Output:
Print Variable Using the printf()
Function
We can also use the printf()
method to print a variable in Java. Unlike the above two ways, when invoked, the printf()
method needs format specifiers (like %d
, %f
, %c
, %s
etc.).
Let us see the syntax for the printf()
method.
Syntax:
System.out.printf(format_specifier, parameter)
As we can see from the syntax, the printf()
method always needs a format specifier to print the variable. If we do not include the format specifier, it will result in an error.
public class Main
{
public static void main(String args[])
{
int x=10;
int y=20;
int res=x+y;
System.out.printf("%d",res);
}
}
Output:
30
Below is what will happen if we do not include the format specifier.
public class Main
{
public static void main(String args[])
{
int x=10;
int y=25;
int res=x+y;
System.out.printf(res);
}
}
Output:
/Main.java:8: error: no suitable method found for printf(int)
System.out.printf(res);
^
method PrintStream.printf(String,Object...) is not applicable
(argument mismatch; int cannot be converted to String)
method PrintStream.printf(Locale,String,Object...) is not applicable
(argument mismatch; int cannot be converted to Locale)
1 error
You can also use the printf()
function to print variables between the texts or strings by using format specifiers.
public class Main
{
public static void main(String args[])
{
int x=10;
int y=20;
int res=x+y;
System.out.printf("The sum of %d and %d is %d.",x,y,res);
}
}
Output:
The sum of 10 and 20 is 30.
Conclusion
This article discussed three unique approaches to print variables in Java. All of the techniques have their advantages, and you can apply them at your convenience.
We would suggest you use the println()
function as it is efficient for simply printing a line of text. If you need your line of text to be formatted or justified, you must always use the printf()
method.
Related Article - Java Print
- Print Contents of Text File to Screen in Java
- Java system.out.println() Method
- Print a Table in Java
- Print New Line in Java