….
🔢 Java Output: Printing Numbers to the Console
You’ve already learned how to print text using System.out.println()
. Now, let’s take it a step further — printing numbers!
At GoNimbus, we teach you not just how to print numbers in Java, but also how Java handles them behind the scenes.
🧮 Printing Numbers
To display numbers in Java, you can simply use System.out.println()
without quotes.
✅ Example:
public class PrintNumbers {
public static void main(String[] args) {
System.out.println(100);
System.out.println(25 + 75);
System.out.println(3.14159);
}
}
🖥️ Output:
100
100
3.14159
❗ Important Difference: Text vs Number
Type | Syntax | Description |
---|---|---|
Text | "100" | Treated as a string (text) |
Number | 100 | Treated as a numeric value (can be used in calculations) |
Example:
System.out.println("100" + "200"); // Text concatenation
System.out.println(100 + 200); // Numeric addition
🖥️ Output:
100200
300
🔎 Notice the difference? One joins the strings, the other performs math!
🔄 Combining Text and Numbers
You can combine both text and numbers in one statement using the +
operator:
System.out.println("The result is: " + 10 + 5);
🖥️ Output:
The result is: 105
📌 Why? Because everything after the first string is treated as more text.
To fix it and get the correct numeric sum, group the numbers inside parentheses:
System.out.println("The result is: " + (10 + 5));
🖥️ Output:
The result is: 15
🧪 Try It Yourself on GoNimbus
Open the GoNimbus Java Editor and type:
public class NumbersDemo {
public static void main(String[] args) {
System.out.println(42);
System.out.println("Age: " + 25);
System.out.println("Total: " + (100 + 50));
}
}
🖥️ Output:
42
Age: 25
Total: 150
🧠 GoNimbus Quick Tips
- Don’t wrap numbers in quotes if you want to perform calculations.
- Use parentheses
()
to control how Java evaluates math expressions. - Java can print integers, decimals (float/double), and results of expressions.
🔜 Up Next: Java Variables and Data Types
Now that you know how to output numbers, let’s explore how to store and manage them using variables.