in the previous post we've covered the basic syntax of java. Now we will see how to get an output from java programming.
🚀 Understanding Output in Java
What is Output?
Output refers to any result displayed on the screen or provided by the computer after performing a task or operation.
For example, if we tell a computer to add two numbers (23 and 24), it will process the operation and return a result:
System.out.println(23 + 24);
💡 Output:
47
This result (47
) is called output.
What is Input?
The data provided to the computer is called input. In the above example, 23
and 24
are inputs, and 47
is the output.
Understanding System.out.println() Breakdown
System.out.println()
is a built-in Java method used to print output to the console. Let's break it down:
System: This is a built-in class in Java that provides access to system-related functionalities.
out: This is a static member of the System class that represents the standard output stream (console output).
println(): This is a method of PrintStream (the type of out) that prints the provided argument and moves the cursor to the next line.
🔹 Printing Output in Java
Unlike Python, Java requires a structured syntax for printing output. Java uses the System.out.println()
method to display output on the screen.
📌 Syntax of System.out.println()
System.out.println(arguments);
Here, arguments refer to the values we want to print.
🔹 Example 1: Printing a Word or Sentence
To print a word or sentence, we enclose it inside double quotes (""
).
System.out.println("Hello, World!");
💡 Output:
Hello, World!
✅ Whatever is inside double quotes is printed exactly as it is.
🔹 Example 2: Printing a Mathematical Calculation
System.out.println(23 + 34);
💡 Output:
57
Java performs the calculation and prints the result.
🔹 Example 3: Printing a Number as Text
If we write numbers inside double quotes, Java will treat them as text (String), not numbers.
System.out.println("23 + 34");
💡 Output:
23 + 34
✅ Java does not calculate it because it's inside double quotes.
🔹 Example 4: What Happens If We Add a Number and a String?
If we try to add a number and a string, Java will concatenate (join) them instead of performing arithmetic.
System.out.println(23 + "34");
💡 Output:
2334
🚨 Why?
-
23
is an integer (number). -
"34"
is a string (text). - Java joins them as text, not as numbers.
🎯 Conclusion
✔ Output is what the computer returns after performing an operation.
✔ Input is the data given to the computer.
✔ Java uses System.out.println()
to display output.
✔ Text must be enclosed inside double quotes ("text"
).
✔ If a number is inside quotes, it is treated as text.
✔ Adding a number and a string concatenates them.
✔ To perform addition, convert the string to an integer using Integer.parseInt()
.
✔ To keep them as text, convert the number into a string using String.valueOf()
.
💡 Want to Learn More?
Follow me for more Java programming tutorials! 🚀
Top comments (2)
Fine article! Please format the first paragraph or run the article through chatgpt just for proofreading.
Also, would also like you to explore printing complex objects (with toString method)
Thank you for your suggestion! This post is just for beginners, like the second step in learning Java. I'll definitely cover printing complex objects with the toString() method in my later posts as we progress. Stay tuned!