南锋

南奔万里空,脱死锋镝余

Java System.out.print 与 println 的区别:换行、输出格式和示例

Java 里常用的控制台输出语句有 System.out.printlnSystem.out.print,两者最主要的区别是输出后是否自动换行。

最明显的区别就是

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