In programming, we often need to show information to the user. For example, we may want to display a message, show the result of a calculation, or tell the user whether something went wrong.
In Java, we can display output using System.out. The two methods you will use most often are print() and println().
Using println()
The most common way to display output in Java is:
System.out.println("Hello World!");
This prints the text Hello World! on the screen.
For example:
class Main {
public static void main(String[] args) {
System.out.println("Hello World!");
System.out.println("Welcome to Java!");
}
}
The output will be:
Hello World!
Welcome to Java!
The important thing about println() is that after displaying the value, it moves to the next line.
Using print()
Java also provides print():
System.out.print("Hello");
The main difference is that print() does not move to the next line.
For example:
System.out.print("Hello ");
System.out.print("John");
Output:
Hello John
Because both messages are printed on the same line.
Compare this with println():
System.out.println("Hello");
System.out.println("John");
Output:
Hello
John
So the easiest way to remember the difference is:
print() → stays on the same line
println() → moves to the next line
Printing Variables
We don't always want to print fixed text. We can also print the value stored inside a variable.
int age = 25;
System.out.println(age);
Output:
25
Similarly:
String name = "John";
System.out.println(name);
Output:
John
Java prints whatever value is currently stored in the variable.
Printing Text and Variables Together
We can also combine text with variables using the + operator.
String name = "John";
int age = 25;
System.out.println("Name: " + name);
System.out.println("Age: " + age);
Output:
Name: John
Age: 25
Here, the + joins the text and the value of the variable together.
We can also use it with calculations:
int age = 25;
System.out.println("Next year you will be " + (age + 1));
Output:
Next year you will be 26
The parentheses are important here because we want Java to perform the addition before joining the result with the text.
Printing Calculations
Java can also calculate values while printing them.
System.out.println(10 + 5);
Output:
15
We can use different mathematical operators as well:
System.out.println(10 - 5);
System.out.println(10 * 5);
System.out.println(10 / 5);
Output:
5
50
2
So System.out.println() isn't limited to printing text. It can also display the result of expressions and calculations.
Printing Different Data Types
We can print values of different data types using the same method.
int age = 25;
double price = 99.99;
char grade = 'A';
boolean passed = true;
String name = "John";
System.out.println(age);
System.out.println(price);
System.out.println(grade);
System.out.println(passed);
System.out.println(name);
Java will display the value of each variable according to its data type.
A Simple Example
Let's put everything together in a small program:
class Main {
public static void main(String[] args) {
String name = "John";
int age = 25;
double score = 89.5;
System.out.println("Student Information");
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Score: " + score);
}
}
Output:
Student Information
Name: John
Age: 25
Score: 89.5
This is how output is commonly used in Java programs to display information to the user.
print() vs println()
The difference is simple:
System.out.print("Hello");
System.out.print("World");
Output:
HelloWorld
While:
System.out.println("Hello");
System.out.println("World");
Output:
Hello
World
So whenever you want the next output to appear on a new line, use println(). When you want to continue printing on the current line, use print().
Once you become comfortable with these two methods, displaying output in Java becomes very straightforward.