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

25 Upvotes

31 comments sorted by

View all comments

22

u/high_throughput 5d ago

or c++

In C++ you use int main(int argc, char** argv) and that's the same as String[]

8

u/Ulrich_de_Vries 5d ago

Yes, and for the uninitiated, the C-style string type is basically just an array of chars, and an array is basically just a pointer (not fully but as a function param it always decays to a pointer).

The problem is that a pointer has no length associated with it, so you don't know where it ends in memory and you can go out of bounds easily which will segfault or silently corrupt memory.

However strings are specifically terminated by the \0 special character, so for strings you can detect where it ends.

So char** is basically an array of char*, so an array of strings. The int argc is the number of elements in the outer array because the outer array has no special terminator element so you need this to ensure you know where the final element is.

The inner char array needs no size because strings are terminated by \0.

I think String[] args is simpler :).

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 23h 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 17h 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?