0

I'm a computer scientist and been programming for a while but I realize that I don't know how e.g. Java does "under the hood" to compute the length of let's say the String array typically called args in public static main(String[] args) {...}. Everyone uses args.length of course but the question is, if one had to program as if only allowed to use imperative procedural static methods (no OO constructs, not even the foreach loop and no try catch blocks), how would you do it?

I was not able to find this anywhere. In C for example, we have the main's arguments as argv and argc since it seems that otherwise it would be impossible to know the length of the argv array (no objects). Of course C also 'magically' populates argc with the length of argv. So my guess is that it's kind of the same with Java but that it puts it in the length attribute of the args object?

However maybe someone can explain what happens more precisely under the hood with this? (I imagine that the command-line somehow parses the arguments since they have to be separated by spaces, so of course, spaces are an indication of how many arguments are passed)

The idea is to do that in a 'procedural' way, juste for the sake of comparison with C.

SheppLogan
  • 322
  • 3
  • 18
  • ok i think it does although they only very briefly mention it. – SheppLogan Feb 20 '21 at 13:08
  • 2
    Your comparison is flawed: C needs the `argc` argument because its arrays are basically identical to pointers and there's no built-in mechanism to store the "true lenght" of an array. In Java an array has length as an inherent property: you can't **have** an array without a length. Therefore, trying to do something "like in C" is not actually getting you closer to "how things really are", because the underlying concepts are different. – Joachim Sauer Feb 20 '21 at 13:09
  • yes I think I see what you mean. Of course C does not have objects and in Java this length attribute is populated by the JVM as I understand. – SheppLogan Feb 20 '21 at 13:19

0 Answers0