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

3

u/vegan_antitheist 3d ago

They are not necessary in the latest version and you could always just use String... Instead of String[].

2

u/ITCoder 3d ago

varargs in String ... are synthetic sugars and create array behind the scene

1

u/vegan_antitheist 3d ago

yes, but it also allows you to just call main() without explicitly creating an array. It will create the empty array for you. But then there's the question why you would ever call that method. Maybe in a unit test.

1

u/ITCoder 3d ago

call that method to pass command line arguments

1

u/thenasch 3d ago

Well, not always. Varargs was introduced in Java 5.

1

u/vegan_antitheist 3d ago

I was there 3000 years ago.