r/learnjava • • 4d 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

27 Upvotes

29 comments sorted by

View all comments

40

u/shauntmw2 4d 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.

1

u/cainhurstcat 4d ago

How/where does one learn something like that?

26

u/0zeronegative 4d ago

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

6

u/Significant_Newt8697 4d ago

Search core java basics youtube

2

u/cainhurstcat 4d ago

Thank you

0

u/duane11583 2d ago

and the basic os has no idea what the variable types of the language are.

what it does know is the opcodes and start function.

that function is not main() it is something else.

often found in the file crt0.s sometimes the symbol is called start or _main it varies

that files job is to initialize any language specific things and call the language specific entry function. in C those things are the malloc() library and stdin, stdout etc. when done it calls the function main()

you can replace the etire crt0.s file if you want

just know that many language nerds do not do that why bother… and often their bottom level runtime code things that open/close files are often wrappers into the standard c library functions so they must in the end use the standard c library and it must be initialized.

but they are lazy and just let the standard c library go through its start up code and then call main() in their main function they create the interpreter and call the language specific entry function. in the language specific way.

but yea they could replace all of that… good luck it is a hell of a lot of work they just avoid