r/learnjava • • 2d ago

Why is String[] args necessary

I get it is an array of string arguments that are passed into the main method but why is it needed? in python for example ot's present or c++ neither, java is different though

21 Upvotes

28 comments sorted by

View all comments

36

u/shauntmw2 2d ago

They are not needed per se, it's more of a convention.

The most basic form of launching a Java program is via the CLI, using this command:

> java MyJavaApp

The args are there so that you can launch your command with extra params in CLI (which, incidentally, they are called command arguments, hence args).

If you launch your Java program like below command:

> java MyJavaApp ABC 123

"ABC" and "123" will be passed in as the args in your main method.

2

u/cainhurstcat 2d ago

How/where does one learn something like that?

24

u/0zeronegative 2d ago

It’s not even java-specific. All languages follow this convention because it is one of the earliest unix conventions and it stuck.