Java中System.out.println与System.out.print的区别与使用场景 | 南锋

南锋

南奔万里空,脱死锋镝余

Java中System.out.println与System.out.print的区别与使用场景

java里常用的控制台输出语句有System.out.println和System.out.print。但是这2者有什么区别了?

最明显的区别就是

System.out.println()输出后追加一个换行
而 System.out.print()输出后不会换行

例如:

1
2
3
4
5
6
7
8
public class ForEachLoop {
public static void main(String[] args){
String[] words = {"Welcome ","to ","myBlog"};
for (String word: words){
System.out.println(word);
}
}
}

输出为:

1
2
3
Welcome
to
myBlog
1
2
3
4
5
6
7
8
public class ForEachLoop {
public static void main(String[] args){
String[] words = {"Welcome ","to ","myBlog"};
for (String word: words){
System.out.print(word);
}
}
}

输出为:

1
Welcome to myBlog
+