Hacker News 中文摘要

RSS订阅

public static void main(String[] args) 已死 -- Public static void main(String[] args) is dead

文章摘要

自2025年9月16日起,Java编程不再需要传统的public static void main(String[] args)入口方法,取而代之的是更简洁的void main()形式。这一变化标志着Java语言的现代化改进,简化了代码结构,提升了开发效率。作者对此表示强烈支持,认为这是对旧有繁琐代码的彻底告别。

文章总结

标题:public static void main(String[] args) 已死

作者:Ethan McCue

自2025年9月16日起,Java编程的入门代码将不再是传统的public static void main(String[] args)。取而代之的是更为简洁的void main()方法。

传统的Java入门代码通常如下:

java public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("What is your name? "); String name = scanner.nextLine(); System.out.println("Hello, " + name); } }

而新的入门代码则简化为:

java void main() { var name = IO.readln("What is your name? "); IO.println("Hello, " + name); }

这一变化标志着Java语言在简化初学者入门难度方面迈出了重要一步。作者对此表示热烈欢迎,并鼓励读者在评论区表达对这一变化的看法,甚至可以尽情释放情绪,无论是尖叫、唱歌还是跳舞。


<- 返回索引

评论总结

评论主要围绕Java 21中引入的“Unnamed Classes and Instance Main Methods”特性展开,讨论了其对Java语言的影响和开发者对此的不同看法。以下是主要观点和论据的总结:

1. 对Java语言简洁性的改进

  • 支持观点:许多评论者认为这一改进减少了Java的样板代码,使其更接近现代编程语言如C#的简洁性。

    • 评论4:“Why not abstract away the public static void main(String[] args) method with a top-level statement paradigm, similar to C#’s entry point simplification, to reduce boilerplate and enhance code conciseness?”
    • 评论15:“C# has top level statements since 4 years ago. Not only you don’t need a class, but you don’t even need a Main() method.”
  • 反对观点:部分评论者认为这种简化可能会让初学者难以理解Java的核心概念,尤其是面向对象编程的基础。

    • 评论9:“Ugly? Sure. But isn’t this the identity of Java? Taken away for almost no gain, there’s literally no real issue stemming from requiring a verbose incantation to write a main routine.”
    • 评论21:“Java is designed with OOP in mind and it kind of makes sense to have the user to think in terms of lego blocks of interfaces.”

2. 对Java历史与身份的讨论

  • 支持观点:一些评论者认为Java的这一变化是语言演进的必然结果,反映了其从C语言继承的简洁性需求。

    • 评论12:“What we seem to have forgotten is just how c like java was compared to other programming languages being advocated at the time.”
    • 评论3:“It’s amazing that ~30 years on, Java is finally becoming a not horrible programming language.”
  • 反对观点:部分评论者认为这种变化可能会削弱Java的独特身份,使其失去原有的特色。

    • 评论9:“It reminds me of the attempt at removing Richard Stallman from the FSF, yeah, you could do that and fix a problem, but then what you are left with isn’t really the same thing as it was before.”
    • 评论19:“If it is just syntactic sugar that has to create a class under the hood, it’s just lipstick on a pig’s turd.”

3. 对开发者实际工作的影响

  • 支持观点:一些评论者认为这一变化对实际开发工作影响不大,尤其是在大型项目中。

    • 评论24:“How main is written is totally irrelevant for my day to day work or my career.”
    • 评论13:“Did it slow down compile times at all to support two different file structures?”
  • 反对观点:部分评论者认为这种变化可能会让初学者在理解Java的基础概念时感到困惑。

    • 评论11:“One thing I’ll miss about this was the way this arcane writing increasingly made sense over time as you became a better programmer.”
    • 评论21:“But, I also agree that can serve as terrible intro to programming if you start programming right away without understanding the basics of abstractions.”

4. 对Java未来发展的期待

  • 支持观点:一些评论者期待Java能够进一步简化语法,引入更多现代编程语言的特性。

    • 评论20:“Could java please also learn that stand-alone functions are cleaner than static methods? even if it’s just syntax sugar...”
    • 评论16:“What I would like to see deprecated is over using objects and design patterns and SOLID and Uncle Bob principles, so we see less FizzBuzz Enterprise Edition and Java feels less as the Kingdom of Nouns.”
  • 反对观点:部分评论者认为Java应保持其原有的设计哲学,避免过度简化。

    • 评论9:“Saving grace is that obviously, you can still recite the incantation, it’s not like public static void main(String[] args) is gone, just that you can skip it.”
    • 评论19:“If you can’t have other top-level functions in Java, then it’s a special case, which is ugly.”

总结:

Java 21的“Unnamed Classes and Instance Main Methods”特性引发了开发者对其简洁性、身份认同和实际影响的广泛讨论。支持者认为这是Java现代化的必要步骤,而反对者则担心这会削弱Java的独特性和初学者对核心概念的理解。