Printing
Learning how to print in Java is critical for developing a variety of programs, but it is also helpful in debugging your program and identifying what is happening on the back-end at different phases. The general syntax for printing is System.out.println()
or System.out.print()
.
System
: a final
class defined in java.lang
out
: an instance of PrintStream
, which is part of the System
class
println
& print
: methods to print to the console
The Difference between println() and print()
println(): adds a line separator (new line) after the printed statement
print(): does not add a new line after the printed statement
Examples
Using println() and print()
public static void main(String [] args) {
System.out.println("Welcome");
System.out.print("Hello ");
System.out.println("World");
}
Output:
Welcome
Hello World
Using Variables with System.out.println()
public static void main(String [] args) {
int a = 4
System.out.println("The value of a is " + a + ".");
}
Output:
The value of a is 4.
Syntax
System.out.println("insert text here");
System.out.print("insert text here");