[ Java 笑话]public static void main(String[] args){}

2022-10-08 08:24:28 +08:00
 yazinnnn

https://www.bilibili.com/video/BV1e24y197Qv/

The classic “Hello World” program looks like this in Java:

public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World"); } } It may only be five lines, but those lines are packed with concepts that are challenging to absorb without already having some programming experience and familiarity with object orientation. Let’s break down the concepts a student confronts when writing their first Java program:

public (on the class). The public accessibility level is relevant only when there is going to be cross-package access; in a simple “Hello World” program, there is only one class, which lives in the unnamed package. They haven’t even written a one-line program yet; the notion of access control — keeping parts of a program from accessing other parts of it — is still way in their future.

class. Our student hasn’t set out to write a class, or model a complex system with objects; they want to write a program. In Java, a program is just a main method in some class, but at this point our student still has no idea what a class is or why they want one.

Methods. Methods are of course a key concept in Java, but the mechanics of methods — parameters, return types, and invocation — are still unfamiliar, and the main method is invoked magically from the java launcher rather than from explicit code.

public (again). Like the class, the main method has to be public, but again this is only relevant when programs are large enough to require packages to organize them.

static. The main method has to be static, and at this point, students have no context for understanding what a static method is or why they want one. Worse, the early exposure to static methods will turn out to be a bad habit that must be later unlearned. Worse still, the fact that the main method is static creates a seam between main and other methods; either they must become static too, or the main method must trampoline to some sort of “instance main” (more ceremony!) And if we get this wrong, we get the dreaded and mystifying "cannot be referenced from a static context" error.

main. The name main has special meaning in a Java program, indicating the starting point of a program, but this specialness hides behind being an ordinary method name. This may contribute to the sense of “so many magic incantations.”

String[]. The parameter to main is an array of strings, which are the arguments that the java launcher collected from the command line. But our first program — likely our first dozen — will not use command-line parameters. Requiring the String[] parameter is, at this point, a mistake waiting to happen, and it will be a long time until this parameter makes sense. Worse, educators may be tempted to explain arrays at this point, which further increases the time-to-first-program.

System.out.println. If you look closely at this incantation, each element in the chain is a different thing — System is a class (what’s a class again?), out is a static field (what’s a field?), and println is an instance method. The only part the student cares about right now is println; the rest of it is an incantation that they do not yet understand in order to get at the behavior they want.

简化后的 hello world

void main() {
    println("Hello World");
}

诶哟谢天谢地,恭喜今后的 java 初学者,终于能学一个正常的 hello world 了

4227 次点击
所在节点    Java
23 条回复
kujio
2022-10-08 08:36:19 +08:00
对初学者来说 void main() {} 和 pubic static void main(String[] args){}只是字数不一样, 这些概念都是在深入学习之后才明白的,而深入学习之后 void main(){} 反而比 public static void main(String[] args) 更难理解。
ZField
2022-10-08 08:45:47 +08:00
初学者没必要一上来就搞简写= =
yolee599
2022-10-08 08:51:45 +08:00
C99 之后,void main() {} 是不规范写法,应该返回 int 的
yolee599
2022-10-08 08:53:19 +08:00
既然是仿 c-style 要仿得像一点嘛
chendy
2022-10-08 09:00:00 +08:00
一众脚本语言:main 是啥?
selca
2022-10-08 09:16:42 +08:00
字数不同而已,打几个字都不愿意,建议学 python ,那个字少
zhuweiyou
2022-10-08 09:25:41 +08:00
不见得是简写,反正有 IDE 都是打几个字母全出来的.
monkeyWie
2022-10-08 09:58:23 +08:00
建议直接学 js ,main 方法都不需要
wdwwtzy
2022-10-08 10:00:08 +08:00
建议直接学 C# ,main 方法都不需要
Kamiyu0087
2022-10-08 10:13:24 +08:00
你用 jshell 一行就够了
一样是 Java
LGA1150
2022-10-08 10:16:31 +08:00
psvm
sout
Leviathann
2022-10-08 10:17:14 +08:00
@LGA1150 main + tab 就可以
WOLFRAZOR
2022-10-08 10:20:40 +08:00
初学开始用简写并不好,容易无法理解。
dbpe
2022-10-08 10:22:10 +08:00
建议学 Node..前后端一把梭
Pastsong
2022-10-08 11:28:32 +08:00
喜欢打字多的建议去写 ASSEMBLY ,那个字多
Slurp
2022-10-08 11:32:59 +08:00
@yolee599 #3 #4 你 C 关我 Java 屁事?

@chendy @zhuweiyou 😅 Python:

if __name__ == "__main__":
angrylid
2022-10-08 12:10:14 +08:00
建议 JavaScript 解释器都不用装,打开浏览器直接写
dreamlike
2022-10-08 12:19:45 +08:00
jshell+system.out.println 不比这个简单?
flyingghost
2022-10-08 12:28:01 +08:00
不想学概念建议口述,电脑都不要。
jorneyr
2022-10-08 13:04:36 +08:00
如果你的世界就只有一个 Hello World ,那么你赢了。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/885128

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX