Printing To The Console in Java
Table of Contents
Introduction
Printing something on the console or panel that displays important messages, such as errors, messages, or data outputs, directly for developers.
In Java, you can print to the console using the System.out.println()
method. Here's a simple example:
System.out.println(100);
prints 100
on the console.
Much of the work done by computer is not visible by default, we use something like System.out.println()
to print if we need some information to be displayed on the console to check for the right data.
Example:
class PrintToConsole {
public static void main(String[] args) {
// Printing a simple message to the console
System.out.println("Hello, world!");
// Printing values
System.out.println(100);
System.out.println(200);
// Printing multiple lines
System.out.println("Printing multiple lines:");
System.out.println("Line 1");
System.out.println("Line 2");
System.out.println("Line 3");
// Printing without a newline character
System.out.print("This ");
System.out.print("is ");
System.out.print("on ");
System.out.print("the ");
System.out.print("same ");
System.out.print("line.");
}
}
The above code prints the following output on the console.
Hello, world!
100
200
Printing multiple lines:
Line 1
Line 2
Line 3
This is on the same line.
As you can see println()
is printing in new line whereas print()
is just printing on the same line.
Assignment
Try to print 200
and string Hello there
.
class Assignment {
public static void main(String[] args) {
// print the `Hello there` message.
// WRITE-CODE HERE
}
}
Look at the above examples if you get stuck.
The answer is System.out.println("Hello there");
.
The final code looks like
class Assignment {
public static void main(String[] args) {
// print the `Hello there` message.
// WRITE-CODE HERE
System.out.println("Hello there");
}
}
Gopi Gorantala Newsletter
Join the newsletter to receive the latest updates in your inbox.