int main(); C-Java 等效项
我是一名大学生,也是java新手。去年第二学期,我们用 C 语言进行了一些编程。在真正理解它之前,我测试了很多东西。在CI中只需要从int main()开始就可以开始编码。 Java 有点令人困惑。 我是否“需要”(我不确定是否必须以这种方式开始)开始
public static void main (String[] args)
为什么,如果我不打算在程序中使用该参数, 。这:
public static void main ()
给我一个错误。我不想读任何争论。
我希望你理解 - 英语不是我的母语。
I'm an University student and new to java. Last year, second semester we did some programming in C. I test a lot of stuff before truly understanding it. In C I only need to start with int main() to start coding. Java is a bit more confusing. Why, do I "need" (I'm not sure if I have to start this way) to start with
public static void main (String[] args)
if I'm not going to use the argument in my program. This:
public static void main ()
gives me an error. I don't want to read in any arguments.
I hope you understand - English is not my Home Language.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这只是语言定义。
签名:
是Java中main方法的签名,有些语言有多个main方法签名。
如果不使用 args 数组,您无需担心,它只会保持为空。
这个 wiki 页面可以让事情变得更清楚一些。
It's just language definition.
The signature:
Is the signature of the main method in Java, some languages have more than one main method signature.
You don't need to worry if you don't use the
args
array, it will just stay empty.This wiki page could clear things up a bit.
C 很古怪。如果将函数声明为
int main()
,则不会检查参数。正确地,不带参数的 C 函数应声明为int foo(void)
,而不是int foo()
。 C main 函数实际上是int main(int argc, char *argv[])
,但由于 C 并不关心你是否将参数放入原型中,所以它会接受int main()。 Java 比 C 更严格(在不止一种方面),并且要求您实际键入其函数的参数,包括
main
。C is quirky. If you declare a function as
int main()
, the arguments aren't checked. Properly, a C function that takes no arguments should be declared asint foo(void)
, notint foo()
. The C main function is reallyint main(int argc, char *argv[])
, but since C doesn't care whether you put the args in the prototype or not, it will acceptint main()
. Java is more of a stickler (in more than one way) than C and requires you to actually type out the arguments to its functions, includingmain
.如果您不读入任何参数,则 args 数组将为空。我不认为有任何技术原因导致你不能像在 C 中那样省略 Java 中的参数,但标准只是规定它必须存在。
If you don't read in any arguments, then the args array will simply be empty. I don't think there's any technical reason why you can't leave out the argument in Java like you can in C, but the standard simply dictates that it has to be there.
在 Java 中,您必须从一个类和一个完整的 main 方法开始。鉴于这只是可能有数千或数百万行代码中的一行,这确实不是一个大问题。
您可以将 IDE 全部包含在类模板中,这样您就不必键入所有这些字符。
如果您想要一个具有最少字符数的有用程序,我建议您尝试像 groovy 或 PHP、Python、Perl 或 bash 这样的脚本语言。
编辑:Java 往往比其他语言更冗长,但是拥有非常简洁的语言也不总是一件好事。 KDB 就是一个例子,
它在一小行中列出了 1 到 R 之间的所有素数。 ;)
In Java you have to start with a class and a full main method. Given this is just one line of code amoungst what could be thousands or millions its really not a big issue.
You can have your IDE full in a class template so you don't have to type all these characters.
If you want to have a useful program with thwe minimum number of characters I suggest you try a scripting language like groovy or PHP, Python, Perl or bash.
EDIT: Java tends to be more verbose than other languages, however having a very terse language is not always a good thing either. One example is KDB
This lists all the prime numbers between 1 and R in one short line. ;)