System.out.println() vs \n in Java

2019-02-02 16:54发布

Let's say I wanted to print 5 lines. Which is the best method (for performance and readability).

System.out.println();
System.out.println();
System.out.println();
System.out.println();
System.out.println();

or

System.out.println("\n\n\n\n");

Is it a matter of preference or is one better than the other. It seems like it would save a lot of time using the second method.

7条回答
走好不送
2楼-- · 2019-02-02 17:25

The 2nd one is definitely less code, performance wise you might not see a difference but the latter may be faster as it is only 1 call but it would be negligible.

查看更多
登录 后发表回答