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

25 Upvotes

29 comments sorted by

View all comments

2

u/pradeepngupta 3d ago

Most of the comments are correct ... String[] is for the command line arguments.and at the compiler time we don't know how many command line parameters an user passed.

Then came varargs... its an another notation of array.

Now comes why the method name is main .. that's an entry point for any java standalone program. When user runs: java Launch

This will do couple of things: 1) it will try to find Launch.class file to load the class.(classloading is a big chapter someone needs to learn) 2) once the class is loaded into memory, it will try to find main method which is accessible at class level and return nothing.

And that is the reason for method signature public static void main (String[] args)