java - Java 中的 Hello World : Understanding the concept versus in python

标签 java python

我现在正在尝试学习 Java,这是 hello world 程序,它已经开始让我感到困惑。我习惯了 python,我发现了 this教程(电子书)简单明了,适合有python背景的程序员。

本书中的 Java 中的 Hello world 程序:

public class Hello {

public static void main (String[] args) {

     System.out.println("Hello World!");
}
}

正如书上所说,python 中的等效代码是:

class Hello(object):
  @staticmethod
  def main(args):
     print "Hello World!"

我完全理解 python 代码。但是,我对 Java 代码有疑问,我想在继续之前弄清楚,以便我在大脑中获得语言的根知识。

这本书说(从书中复制):

...we have one parameter. The name of the parameter is args however, because everything in Java must have a type we also have to tell the compiler that the value of args is an array of strings. For the moment You can just think of an array as being the same thing as a list in Python. The practical benefitt of declaring that the method main must accept one parameter and the parameter must be a an array of strings is that if you call main somewhere else in your code and and pass it an array of integers or even a single string, the compiler will flag it as an error.

这对我来说没有任何意义。为什么我不能传递任何东西,因为我的函数不需要任何东西?如果我只是通过 (String args) 会发生什么。

由于我是 Java 的新手,请多多包涵。

最佳答案

好吧,你正在传递一些东西。每当您运行 Java 程序时,命令行参数都会作为 args 参数传入,因此您需要接受这些参数,即使您不使用它们也是如此。

关于java - Java 中的 Hello World : Understanding the concept versus in python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10215225/

相关文章:

java - 如何在managedBean之间传递参数

java - 如何在新版本的 Android Studio 中添加类路径

java - 如何从文档监听器更新 JComboBox 的列表?

python - 为具有多个输入和输出的函数并行化 for 循环

python - 找到天数滞后并将 0 替换为最后一天的滞后 Pandas

java - 不同语言的 Assets

java - 如何在 Axis2 WebService 中通过用户名和密码进行身份验证?

python - 为什么重置python全局值不生效

python - 从 git bash for windows 激活 venv

python - Pandas :搜索并将值添加到多列的单元格