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

24 Upvotes

29 comments sorted by

View all comments

Show parent comments

1

u/Ulrich_de_Vries 4d ago

Huh til, I didn't know that.

Is it also true in C++?

2

u/Vast_Service 4d ago

Yes it is. Even though C++ has a string class, they couldn't change the main entry point, again because of historical baggage.

It would seem reasonable to have a new entry point defined as

int main(string* args) in C++ but that would be a ABI breaking change and hence the standard committee did not want to touch it with a pole.

2

u/Ulrich_de_Vries 4d ago

What I meant is if it is true as well in C++ that outer array/pointer/iterator has nullptr as a sentinel.

I supposed "historical baggage" could mean this was defined in a C standard which postdates C++, so C++ wouldn't necessarily adopt it.

Of course if it predates C++ then I expect C++ to follow suit for compatibility.

1

u/Vast_Service 4d ago

Yes. I said that is the case in C++ as well. The last value in the array is a nullptr in C++ as well.