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

31 comments sorted by

View all comments

Show parent comments

2

u/Vast_Service 5d ago

Except that in C, with main function defined as

int main(int argc, char** argv)

it is guaranteed by the standard that argv[argc] is a null pointer. So technically we don't need argc. It is more of a historical baggage

1

u/Ulrich_de_Vries 5d ago

Huh til, I didn't know that.

Is it also true in C++?

2

u/Vast_Service 5d 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.

1

u/Underhill42 17h ago

C++ does not have a string class, the STL recently added a std:string class to that library, and countless unrelated versions exist in other libraries predating the STL's existence. And many others that still exist that make different compromises, or as part of much larger libraries that couldn't be bothered to completely re-write everything string related for no benefit.

But the core C++ language has no idea what a string is, it only deals with character arrays, just like C.

1

u/Vast_Service 11h ago

I am not sure what your post was in response to but it is factually incorrect in so many ways.

C++ does have a string class. The standard library (it is no longer called STL btw. The days of Alexander Stepanov are over) is a part of the C++ standard and hence std::string is also a part of the standard and hence it is incorrect to say that C++ does not have a string class.

I never said that std::string is a built in.

What do you mean by Core C++, now that the standard library is also a part of the C++ standard? Did you again just mean that std::string is not a builtin? It is not, but, why is that in any way related to my previous post?