Print statements
How to Display output on the screen in Java?
In Java, when we want to display output on the screen, we use the print()
and println()
methods. These methods are part of something called System.out, which simply means “send the output to the screen.”
Difference Between print()
and println()
print()
– This prints the text and keeps the cursor on the same line.
public class printStatements{
public static void main(String[] args) {
System.out.print("Hello ");
System.out.print("World!");
}
}
Output: Hello World!
- The words stay on the same line because
print()
does not move to the next line.
2. println()
– This prints the text and moves to the next line.
public class printStatements{
public static void main(String[] args) {
System.out.println("Hello ");
System.out.println("World!");
}
}
Output:
Hello
World!
- Here, “World!” appears on a new line because
println()
moves the cursor to the next line after printing.
What is the difference between the print() and println() methods in Java?