The Difference Between print and println
The visible difference is simple: println adds a line terminator after its value, while print leaves the cursor on the current line.
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 | String[] words = {"Welcome", "to", "myBlog"}; |
Output:
1 | Welcome |
Using print with spaces in the output:
1 | for (String word : words) { |
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.