开发者

Java prints output twice in a very specific circumstance

开发者 https://www.devze.com 2022-12-11 01:13 出处:网络
System.out.print(\"My string: \"); My string: BUILD SUCCESSFUL (total time: 1 second) System.out.print(\"My string\");
System.out.print("My string: ");

My string: BUILD SUCCESSFUL (total time: 1 second)

System.out.print("My string  ");

My string BUILD SUCCESSFUL (total time: 1 second)

System.out.print("My string:  ");

My string: 开发者_开发知识库My string:

BUILD SUCCESSFUL (total time: 1 second)

Printing a string that ends with a colon and two spaces will make it print twice. What is happening here?


Edit: looks like it's a bug in Netbeans indeed

http://netbeans.org/bugzilla/show_bug.cgi?id=167279


I think the output stream's buffering is making you interpret what you see wrongly. Try using println().


I noticed that behavior on Netbeans, when running JUnit tests. It that also your case? Seems to be just a bug.


Which java version do you have? It doesn't happen in 1.6.0_16

Are you sure it's not some danging lines in the unix shell (if you're on unix)?. Try with System.out.println("My string: ");

0

精彩评论

暂无评论...
验证码 换一张
取 消