Print Variables
Displaying variable values on the screen
🖨️ What is Printing Variables?
Printing variables means displaying their values on the screen using System.out.println(). This helps you see what data your variables contain and debug your programs effectively.
public class PrintExample {
public static void main(String[] args) {
String name = "Alice";
int age = 22;
double height = 5.6;
System.out.println(name); // Print variable directly
System.out.println(age);
System.out.println(height);
}
}
Output:
Alice
22
5.6
Ways to Print Variables
println()
Prints and moves to next line
System.out.println("Hello");
System.out.println("World");
print()
Prints on the same line
System.out.print("Hello ");
System.out.print("World");
String Concatenation
Combine text with variables
String name = "John";
System.out.println("Hello " + name);
Multiple Variables
Print several variables together
int a = 5, b = 10;
System.out.println(a + " + " + b + " = " + (a+b));
🔹 Basic Variable Printing
The simplest way to print variables is to put them inside System.out.println():
public class BasicPrint {
public static void main(String[] args) {
// Create variables
int number = 42;
String text = "Java Programming";
double decimal = 3.14;
boolean flag = true;
// Print each variable
System.out.println(number);
System.out.println(text);
System.out.println(decimal);
System.out.println(flag);
}
}
Output:
42
Java Programming
3.14
true
🔹 Printing with Labels
Add descriptive text to make output more readable:
public class LabeledPrint {
public static void main(String[] args) {
String studentName = "Emma";
int studentAge = 20;
double gpa = 3.85;
boolean isEnrolled = true;
// Print with descriptive labels
System.out.println("Student Name: " + studentName);
System.out.println("Age: " + studentAge + " years old");
System.out.println("GPA: " + gpa);
System.out.println("Currently Enrolled: " + isEnrolled);
}
}
Output:
Student Name: Emma
Age: 20 years old
GPA: 3.85
Currently Enrolled: true
🔹 Print vs Println
Understanding the difference between print() and println():
public class PrintVsPrintln {
public static void main(String[] args) {
String first = "Hello";
String second = "World";
// Using println() - each on new line
System.out.println("Using println():");
System.out.println(first);
System.out.println(second);
// Using print() - same line
System.out.println("\nUsing print():");
System.out.print(first);
System.out.print(" ");
System.out.print(second);
System.out.println(); // Add new line at the end
// Mixed approach
System.out.print("Mixed: ");
System.out.println(first + " " + second);
}
}
Output:
Using println():
Hello
World
Using print():
Hello World
Mixed: Hello World
🔹 Printing Calculations
You can print the results of calculations directly:
public class PrintCalculations {
public static void main(String[] args) {
int a = 15;
int b = 7;
// Print individual variables
System.out.println("First number: " + a);
System.out.println("Second number: " + b);
// Print calculations
System.out.println("Sum: " + (a + b));
System.out.println("Difference: " + (a - b));
System.out.println("Product: " + (a * b));
System.out.println("Division: " + (a / b));
// Print formatted equation
System.out.println(a + " + " + b + " = " + (a + b));
}
}
Output:
First number: 15
Second number: 7
Sum: 22
Difference: 8
Product: 105
Division: 2
15 + 7 = 22