我现在正在尝试学习 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/