I was recently asked in an exam why we use main (String args[ ]) only in java? Why don't we use main (String args[ ]) in any other programming languages?
main() and other ways to shape the entry point into the application is a part of the language. The reason it is done the particular way in particular language is because the author of the language has seen the protocol between the language runtime system (an OS abstraction layer if that language needs to have an OS underneath to run) and the programmer that way.
I can assume that in Java it was made main(String[] args) because the language was inspired by C++ in a way (thus main() was preserved) and String[] args this is the Java way to pass an array of strings into the function.
Regarding…
“Why don't we use
main (String ars[])in any other programming languages?”
That’s a flawed assumption in the question.
C# example from MSDN:
// Hello3.cs
// arguments: A B C D
using System;
public class Hello3
{
public static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
Console.WriteLine("You entered the following {0} command line arguments:",
args.Length );
for (int i=0; i < args.Length; i++)
{
Console.WriteLine("{0}", args[i]);
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With