OUTPUT IN JAVASo, fellow monkeys we learned taking input, now let's talk about giving output on screen. We are already familiar with print though, i.e:
System.out.println("Hello, World!");
This same line is used for every output but with a bit modifications for different purposes. They are as follow:
1. SIMPLE OUTPUT:
System.out.print("Hello, World!");
System.out = System gives output
print = Print the content ahead
("Hello, World") = The content inside bracket is printed.
System.out.print("Hello");
System.out.print("World!");
This will print HelloWorld . Therefore we need a way to print the content on different lines for better representation.
2. OUTPUT WITH NEXT LINE:
System.out.println("Hello, World!");
println = Print the content ahead and go to next line
3. OUTPUT WITH VARIABLE:
System.out.println("Your age: " + age);
"Your age: " = Prints this as it is
+ = join the content on right to content on left (Called concantenation)
age = joins whatever is inside in variable as it is no matter what datatype
So final output is Your age: 20
Using formated output you can output anything in specific design. Here the concatenation using + does not work. Here is a demonstration:
System.out.printf("Your age: %d" , age);
"Your age: " = Prints this as it is
%d = reserve space for integer variable
, = Tells java to place the variables on right in reserved spaces
age = Variables that is to be placed in reserved places
So final output is Your age: 20
To reserve space for each variable a placeholder for that specific datatype is used. Here is a list for them all:
Placeholders for each datatype:
%d → Integer placeholder
%f → Double / Float placeholder
%c → Character placeholder
%s → String placeholder
%b → Boolean placeholder
Extra things for styling:
\n → Since println does not work here we use \n for next line.
\t → Prints tab space
\" → Since quotations are used to represent string, so to print quotation inside string use backslash with them
\\ → Prints tab space similarly for printing backslash use another backslash with it
Let's write a simple code for all....
public class OutputDemo {
public static void main(String[] args) {
// Variables for each datatype
int age = 20;
double gpa = 3.75;
char grade = 'A';
String name = "Hassan";
boolean pass = true;
// Demonstrating placeholders with escape sequences
System.out.printf("Student Details:\n");
// \n = next line
// %s for string
System.out.printf("Name:\t%s\n", name);
//prints tab space + name + new line
// %d for integer
System.out.printf("Age:\t%d\n", age);
//prints tab space + age + new line
// %f for double
System.out.printf("GPA:\t%.2f\n", gpa);
// prints tab space + GPA + new line (%.2f = 2 numbers after decimal)
// %c for character
System.out.printf("Grade:\t%c\n", grade);
//prints tab + grade + new line
// %b for boolean
System.out.printf("Passed:\t%b\n", pass);
//prints tab + pass +new line
// Special escape sequences
System.out.printf("Quote Example:\t\"Work hard, dream big!\"\n");
//prints Quote Example: "Work hard, dream big!"
System.out.printf("Backslash Example:\tC:\\Users\\Hassan\n");
//prints Quote Example" C:\Users\Hassan"
}
}
CLOSING
That’s it for Output in Java! We now know how to make our program talk back, whether it’s a single word, a full sentence, or a nicely formatted report.
Before jumping into the next blog (if-else statements), it’s time to practice. I’ve prepared an exercise and a mini-project that cover everything you’ve learned in the first six posts.
📂 Basics Exercises Folder (For Posts 01-06)
🌐 Full Java Fundamentals GitHub Repo
Download the exercise, break it down line by line, and then try the project challenge. This will make sure you really “own” the concepts before moving forward.
0 Comments