public static void main(String sfd[]) {
System.out.println("Hello World");
}
public static void main(String[] sfd) {
System.out.println("Hello World");
}
这两种写法语法检测器与编译器都能正常通过,不影响执行结果。但为了避免语义歧义,一般建议这种常规的写法:
public static void main(String[] args) {
System.out.println("Hello World");
}
java.io.PrintStream#println(java.lang.String)
//System.out.println("Hello World");
public void println(String x) {
synchronized (this) { //同步锁,意味着是线程安全的
print(x);
newline();
}
}
相关API如下:
java.io.PrintStream#println(java.lang.String)
public void print(String s) {
if (s == null) {
s = "null";
}
write(s);
}
java.io.PrintStream#write(java.lang.String)
private void write(String s) {
try {
synchronized (this) { //同步锁,是线程安全的
ensureOpen();
textOut.write(s);
textOut.flushBuffer();
charOut.flushBuffer();
if (autoFlush && (s.indexOf('n') >= 0))
out.flush();
}
}
catch (InterruptedIOException x) {
Thread.currentThread().interrupt();
}
catch (IOException x) {
trouble = true;
}
}
java.io.PrintStream#newline
private void newline() {
try {
synchronized (this) { //同步锁,是线程安全的
ensureOpen();
textOut.newline();
textOut.flushBuffer();
charOut.flushBuffer();
if (autoFlush)
out.flush();
}
}
catch (InterruptedIOException x) {
Thread.currentThread().interrupt();
}
catch (IOException x) {
trouble = true;
}
}
一层一层看API函数源码,可得知这句简单的"HelloWorld"的输出是线程安全的。
记录与总结,2021年 11月 25日 星期四 01:03:24 CST。



