关于System.out.println()与System.out.print("\n")的区别

这是在写junit测试的时候发现的。

 1 import java.io.ByteArrayOutputStream;
 2 import java.io.PrintStream;
 3 
 4 public class Test {
 5     public static void main(String[] args) {
 6         PrintStream out=System.out;
 7         ByteArrayOutputStream outContent = new ByteArrayOutputStream();
 8         System.setOut(new PrintStream(outContent));
 9         System.out.println("hello");
10         System.setOut(out);
11         System.out.println(outContent.toString().equals("hello\n"));
12         outContent.reset();
13         System.setOut(new PrintStream(outContent));
14         System.out.print("hello\n");
15         System.setOut(out);
16         System.out.println(outContent.toString().equals("hello\n"));
17     } 
18 }

上面这段程序输出的结果是false true。这意味着System.out.println()与System.out.print("\n")输出的字符还是有差别的。如果要将输出重定向到str用于测试是否相等的话,需要注意这一点。

转载于:https://www.cnblogs.com/fuji/p/4666172.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值