Now that you’ve learned how to store values using variables, let’s talk about how to display them on the screen. In Java, the System.out.println() method isn’t just for static text — it’s your tool for outputting dynamic data, too!

At GoNimbus, we help you understand how to print variables clearly and efficiently.


📌 Displaying Text with Variables

To print the value of a variable, simply pass it inside the System.out.println() method:

String language = "Java";
System.out.println(language);

🖥️ Output:

Java

You can also combine variables and text using the + operator (called concatenation):

String name = "GoNimbus";
System.out.println("Welcome to " + name + " tutorials!");

🖥️ Output:

Welcome to GoNimbus tutorials!

👥 Combine Multiple String Variables

String firstName = "John";
String lastName = "Doe";
String fullName = firstName + " " + lastName;

System.out.println("Full Name: " + fullName);

🖥️ Output:

Full Name: John Doe

✅ Notice the " " (space) between the two names — Java doesn’t automatically insert spaces, so you need to add them manually.


➕ Print Numeric Variables and Perform Math

When using the + operator with numeric values, Java performs mathematical addition:

int a = 10;
int b = 15;
System.out.println("Sum: " + (a + b));

🖥️ Output:

Sum: 25

🧠 Wrap the operation in parentheses to ensure Java adds the numbers before combining with the string. Without parentheses, it will concatenate values as text!

System.out.println("Result: " + a + b);  // Output: Result: 1015 (wrong)
System.out.println("Result: " + (a + b)); // Output: Result: 25 (correct)

🔄 Mixing Strings and Numbers

Java lets you mix strings and numbers in output, but be careful with how they’re combined:

int age = 30;
System.out.println("Age: " + age); // Works fine

But this can be misleading:

System.out.println("Total: " + 10 + 5); // Output: Total: 105

Use parentheses to ensure proper math:

System.out.println("Total: " + (10 + 5)); // Output: Total: 15

💬 GoNimbus Tips

✅ Always use parentheses when mixing numbers with strings to avoid unexpected results
✅ Use spaces " " explicitly when joining words or values
✅ Use descriptive variable names like userName, totalScore, courseTitle for readability


🔜 Coming Next: Java Data Types

You’ve learned how to store and display variables — now let’s understand the different types of data Java can handle and how to choose the right type for your needs.


Scroll to Top