Java中System.out.println和System.out.print的区别

2023-02-23 09:00:25 浏览数 (1)

(adsbygoogle = window.adsbygoogle || []).push({});

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

最明显的区别就是

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

例如:

代码语言:javascript复制
public class ForEachLoop {
    public static void main(String[] args){
        String[] words = {"Welcome ","to ","myBlog"};
        for (String word: words){
            System.out.println(word);
        }
    }
}

输出为:

代码语言:javascript复制
Welcome
to
myBlog
代码语言:javascript复制
public class ForEachLoop {
    public static void main(String[] args){
        String[] words = {"Welcome ","to ","myBlog"};
        for (String word: words){
            System.out.print(word);
        }
    }
}

输出为:

代码语言:javascript复制
Welcome to myBlog

0 人点赞