Variables in Java are known as “storage units” since they store values for the execution of code. The values in the variables can be easily modified while coding. Variables can be declared as local, instance, and static as per the nature of the code. The values of the variable can be printed in Java using multiple methods.
This article will dive into multiple methods to display/print the variables in Java.
How to Print/Display Variables in Java?
The variables in Java store the data and are declared before the usage. The variables in Java are declared using two main parameters which are the data type and the name of the data. Let us take an example to understand String str1 = “Code”; here the data type is a string and the data name is str1 along with the value that is “Code”. Various methods in Java help to print the variable along with the values as listed below.
- The println() method
- The printf() method
- Using print() Method with concatenation
Method 1: Using println() Method
The println() method prints the variable on the screen as the code below demonstrates the implementation. The println() method can be used with or without an argument.
Example 1: Using println() to Print the Variables
Here is a practical demonstration of how variables can be printed in Java using the stated method.
class Example1 {
public static void main(String[] args) {
int y = 15;
int z = 5;
int a = y * z;
System.out.println("The result for variable 'a' is: " + a);
}
}
In the above code:
- The class is declared as “Example1”.
- The integers are declared as “y”, “z” and “a”.
- The println() method of Java prints the variable along with its value respectively.
Output
The output below shows that the value of the variable “a” is printed as “75”.
Example 2: println() Method With StringBuilder Class to Print the Variables
The StringBuilder class in Java helps to generate a mutable sequence of characters, unlike the string class that generates an immutable sequence of characters. The code below shows how the variables are displayed using the StringBuilder class.
class Example2 {
public static void main(String[] args) {
//Declare a string in a variable
String name = "John";
StringBuilder strb = new StringBuilder();
//The append() method to add the output
strb.append("Variable 'name' is: ");
strb.append(name);
System.out.println(strb.toString());
}
}
In the above code block:
- The class is declared as “Example2”.
- In the next step, the data type is specified as the string with a variable name as “name”.
- The StringBuilder() constructor in Java specifies a string builder that caters to a capacity of 16 characters in total.
- The append() method adds the string to the declared string “name”.
- The toString() method in the last step returns the value provided by the StringBuilder object “strb”.
- The variable along with its value is printed using the println().
Output
The output below shows that the value “John” of the variable “a” is printed using StringBuilder.
Method 2: Using printf() Method
The printf() method in Java uses different specifiers such as %s and %d to print the variable, as demonstrated in the following example.
class Example2 { //main() method of Java
public static void main(String[] args) {
String name = "John";
int ID = 525;
System.out.printf("The Employee 'name' is %s and 'ID' is %d", name,ID);
}
}
In the above Java code:
- The string variable is declared as “name”.
- The integer is declared as “ID” with a value of “525”.
- The printf() method uses the variable “name” using the specifier “%s” and the integer “ID” as “%d” to print the variable as the output.
Output
The values of the variables declared as “name” and “ID” are printed as “John” and “525” respectively.
Method 3: Using print() and Concatenation
The print() method takes an argument and prints the variables using the “ + ” operator to concatenate the variables with the strings. The code below implements the print() method with concatenation.
class Example3 {
public static void main(String[] args) {
int y = 10;
int z = 5;
int a = y + z;
System.out.print("The result for variable 'a' is: " + a);
}
}
The code above shows:
- Three integers are declared as “ y ”, “ z ”, and “ a ”.
- The variable “a” is printed using concatenation with the string using the print() method.
Output
The output below shows the value “15” which is basically the sum of variables “ y ”, and “ z ” for the variable “ a ”.
Error When No Arguments are Passed in print() Method of Java
The code below shows that the print() method of Java is declared without an argument which eventually results in an error.
class ErrorExample {
public static void main(String[] args) {
int y = 10;
int z = 5;
int a = y + z;
System.out.print();
}
}
Output
The output results in an error “no suitable method found for print(no arguments)” as shown below.
This sums up the implementation of certain methods to print the variables in Java.
Conclusion
In Java, the variables along with the declared values can be printed or displayed using different methods such as the “println()” method, the “printf()” method, and the print() method. This write-up has effectively elaborated all these methods with practical examples to display/print the variables in Java.