Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we use (String args[ ]) only in Java? [closed]

Tags:

java

c++

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?

like image 830
mahbub_siddique Avatar asked May 15 '26 16:05

mahbub_siddique


2 Answers

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.

like image 64
bobah Avatar answered May 17 '26 05:05

bobah


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]); 
      }
   }
}
like image 32
Cheers and hth. - Alf Avatar answered May 17 '26 07:05

Cheers and hth. - Alf



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!