Nanfeng

Notes on software development, code, and curious ideas

System.out.println vs. System.out.print in Java

The visible difference is simple:

  • System.out.println() writes its value and then ends the line.
  • System.out.print() writes its value without automatically ending the line.

Using println:

1
2
3
4
5
String[] words = {"Welcome", "to", "myBlog"};

for (String word : words) {
System.out.println(word);
}

Output:

1
2
3
Welcome
to
myBlog

Using print with spaces in the output:

1
2
3
for (String word : words) {
System.out.print(word + " ");
}

Output:

1
Welcome to myBlog 

For formatted output, System.out.printf() is often more convenient:

1
System.out.printf("Name: %s, score: %d%n", name, score);

%n emits the platform-appropriate line separator.

+