System.out.println() and System.out.print() in Java: Detailed Explanation
Console output is a fundamental part of Java programming, and System.out.println()
and System.out.print()
are essential tools for this purpose. Let’s break down what they are, how they work, and what the differences are.
1. What Are System.out.println() and System.out.print()?
In Java, the object System.out
is used to display data on the screen. This object is the standard output stream, and by default, it prints text to the console. It has two main methods:
System.out.print()
– prints text to the console without moving to a new line.System.out.println()
– prints text to the console and moves the cursor to a new line after printing.
Example of using print()
public class PrintExample {
public static void main(String[] args) {
System.out.print("Hello");
System.out.print(" world!");
}
}
Console output:
Hello world!
Both strings are printed on the same line since print()
doesn’t move the cursor to a new line.
Example of using println()
public class PrintlnExample {
public static void main(String[] args) {
System.out.println("Hello");
System.out.println("world!");
}
}
Console output:
Hello
world!
println()
automatically moves the cursor to the next line after printing.
2. When Does a Line Break Occur?
A new line is created in Java when:
- Using
System.out.println()
, as this method adds a\n
character at the end of the output. - Manually adding
\n
insideprint()
, for example:System.out.print("Hello\n"); System.out.print("world!\n");
Output:Hello world!
This code works similarly toprintln()
.
3. Understanding System.out.println(): What Is What?
Breaking down System.out.println("Hello");
:
System
– a predefined class from thejava.lang
package.out
– a static field of theSystem
class, representing an instance of thePrintStream
class.println()
– a method of thePrintStream
class that prints text to the console.
Detailed Breakdown
a) System
– The Class
System
is a predefined class from the java.lang
package. It provides access to standard input, output, and error streams, along with other utility methods, such as:
System.out
– the standard output stream.System.in
– the standard input stream (usually the keyboard).System.err
– the standard error stream.System.exit(0)
– terminates the program.
Why don’t we need to import
System
?
TheSystem
class is injava.lang
, which is automatically included in every Java program.
b) out
– The PrintStream
Object
Inside System
, there is a static field out
, which is an instance of the PrintStream
class.
In Java’s source code, you can find:
public static final PrintStream out;
This means:
- The field
out
is static (static
), so we call it asSystem.out
without creating aSystem
object. - The field is
final
, meaning it cannot be reassigned. PrintStream
is a special class that handles printing data to the console.
c) println()
– Method of the PrintStream
Class
The println()
method performs two tasks:
- Prints the provided text to the console.
- Adds a new line (
\n
) at the end.
In the PrintStream
class, println()
is implemented like this:
public void println(String x) {
synchronized (this) {
print(x);
newLine();
}
}
print(x);
– prints the text without a new line.newLine();
– moves the cursor to a new line.
4. Common Errors and Nuances
- Missing Space with
print()
System.out.print("Hello"); System.out.print("world!");
Output:Helloworld!
(words are merged).
Corrected version:System.out.print("Hello "); System.out.print("world!");
- Unexpected Output When Concatenating Strings and Numbers
System.out.println("5 + 3 = " + 5 + 3);
Output:5 + 3 = 53
(because"5 + 3 = "
is a string, so5 + 3
are converted into strings).
Correct version:System.out.println("5 + 3 = " + (5 + 3));
Output:5 + 3 = 8
. - String Literal Error
System.out.println("Hello world!);
❌ Compilation error: “unclosed string literal” (missing closing quote"
). - Printing an Empty Line
System.out.println();
✅ Allowed, simply moves the cursor to a new line.
5. When to Use print vs println?
Method | Functionality | Line Break? |
---|---|---|
System.out.print() | Prints text | ❌ No |
System.out.println() | Prints text and moves to a new line | ✅ Yes |
6. Where Does the System.out
Object Come From?
Here’s an approximate example of how the JVM sets up System.out
:
PrintStream myOutput = new PrintStream(System.out);
System.out = myOutput; // (in reality, out is final)