• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why was String[] used as an argument in main method?  RSS feed

 
Raj Soni
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am aware that JVM starts executing a program from main method having String[] as argument. But was there any specific reason that JVM was designed in such way? If it was just a matter to execute a program it could also be done by only main() method with out arguments like in C.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Twenty years after the decision was taken, one can only guess. Unless you happen to meet James Gosling or similar.
You know that in C you pass arguments to the command line, and you can get them with
int main(int argc, char **argv)
{
    . . .
}

Well, as you doubtless already know you can pass similar arguments, which are in the form of text (hence String[]), to the Java® runtime. Obviously they thought for reasons of consistency they would only have one version of main method. If you pass nothing you simply get a zero‑element array. Remember that Java® array handling is so much more robust than C's and Java® arrays are actually objects.
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
More to what Cambell Ritchie already explained to you.

Raj Soni wrote:I am aware that JVM starts executing a program from main method having String[] as argument.

1. Method "main" has parameters, not arguments. Arguments are being passed to the method when invoking the method - and these are values. And parameters defines these expected arguments. You could see that these are being used interchangeable, unfortunately quite often wrong.
Example:

2. Also "main" method parameters could be in a form "public static void main(String... args)". String followed by ellipsis defines variable arguments, so it means it can accept either an array of Strings or single or multiple values each of them separated by comma.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so what other choice do you have? You could restrict it to not allow passing in anything, but there are times when you want to pass in something.

You could pass in a single string, but again...that limits you.

You could pass in an integer...but you can't force the user to always input a properly formed one.

A String array allows you to take ANYTHING the user types on a keyboard (ok...maybe not quite anything, but almost anything). You can pass as few or as many arguments as you want. It will work with (i believe) any keyboard in any language.

What other thing might you want to pass in that would work so well?
 
Tim Holloway
Saloon Keeper
Posts: 18799
74
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The original C construct was:


Sometimes coded as "*argv[]".

Early C compilers did not strictly enforce checking, so you'd see a lot of old programs that declared:


But modern compilers will generally flag with warnings for lack of return type and proper arguments.

Besides, on most OS's, there's ALWAYS at least one argv value and argc is thus 1 or greater. That's because argv[0] is a string representing the command line that invoked the program for C programs.

Java doesn't use pointers, so its equivalent to "**argv" would be String[], and argc isn't necessary, since in java it's the same value as argv.length.

Don't forget also that optional/varargs constructs were not present in the original Java implementation. They didn't arrive until about Java 1.4 or 1.5 and I imagine that refusal to support overloading on the main() function made life simpler for the JVM. Especially if it would have had to deal with several choices for main(), depending on the command line. Instead, they normalized it.

 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Holloway wrote: . . . optional/varargs constructs were not present in the original Java implementation. They didn't arrive until about Java 1.4 or 1.5 . . .
In Java5.

Since the compiler doesn't distinguish Foo[] from Foo... as parameter types, use of varargs does not constitute overloading. As you said, it is, instead, normalisation.
 
Moorthi Duruvasan
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In java array consider as an object
In java everything consider as an object
In java main method accepts only string array as an parametter we can't break this rules because of already defined like that only jvm....jvm is hardcoded
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
Moorthi Duruvasan wrote:In java array consider as an object
In java everything consider as an object
. . .
Not quite. What about primitives?
 
Tim Holloway
Saloon Keeper
Posts: 18799
74
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For that matter, unless I've forgotten something, in Java, an array is NOT a class, nor is it derived from java.lang.Object.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Holloway wrote:For that matter, unless I've forgotten something, in Java, an array is NOT a class, nor is it derived from java.lang.Object.


I'm not sure whether Arrays are classes (they are dynamically created types), but they definitely do derive from Object. An Object reference can have any array assigned to it (no matter what the component type is), and from the Java Language Specification 10.1 Array Types:

The direct superclass of an array type is Object.


Serializable and Cloneable are also supertypes of array types.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Welcome to the Ranch
Moorthi Duruvasan wrote:In java array consider as an object
In java everything consider as an object
. . .
Not quite. What about primitives?

or operators?
 
Tim Holloway
Saloon Keeper
Posts: 18799
74
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It gets a little muddy here, since there's an "array of X" and then there's java.util.Array. I did a quick check, however, and yes, an array does support the methods of java.lang.Object, including getClass. However, I found a note that aArray.equals(bArray) only returns true if aArray == bArray, not if the array contents and lengths are equal.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's no doubt that arrays are special because they support the [] operator and have special declaration and initialization syntax.

I didn't see any mention in the JLS of what the relationship between an array type and java.util.Array is though.
 
Puspender Tanwar
Ranch Hand
Posts: 499
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in my opinion , JVM gives preference to String[] because every type(primitive, object, operators etc.) can be written in the form of strings and we can get the respective types by casting or the inbuilt methods provided by JAVA
or I would say that string has got superpowers to hold every type in it by simply concatenation
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, it is not that a String has superpowers. But Strings are used frequently so the Object class has a toString method which means every object you see can be turned into a String with its own toString method. Of course the unoverridden version give a rather ugly output, so you will need to override it.
The + operator is overridden on Strings so you can catenate any object or any primitive or null to any Strings.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course there is a class behind an array. In the case of ints it is called [I and in the case of Strings it is called [Ljava.lang.String; They must be created dynamically because you can never find the code for them.
You can get that simply:-
You can also see that you can assign those arrays to an Object reference, which shows that the array classes do extend Object. You can of course verify that with reflection:-
System.out.println(ob.getClass().getSuperclass().getName()); // java.lang.Object

There is no creation relation between an array and java.util.Arrays, however. If you look up the Arrays class, it says this:-
The Arrays class documentation wrote:This class contains various methods for manipulating arrays
The more you find out about the Arrays class, the better, because it has some really useful things in. If you find the Array class, you find it is for use in reflection, and again has nothing to do with creating arrays, though it does have newInstance methods which you can use as factory methods. You may never use the Array class in ordinary Java® programming.

Are we straying from the original subject of the question?
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Earlier today, I wrote: . . . array classes do extend Object. You can of course verify that with reflection:-
System.out.println(ob.getClass().getSuperclass().getName()); // java.lang.Object
. . .
It prints always java.lang.Object regardless of whether the element of the array extends Object or whether it extends another class.

I think I was mistaken about there being classes for arrays. It says this in the JLS
The Java® Language Specification wrote:Although an array type is not a class, the Class object of every array acts as if:
  • The direct superclass of every array type is Object.
  • Every array type implements the interfaces Cloneable and java.io.Serializable.
  • That does imply that there is a Class<[Foo> object for each array as shown in the code I posted earlier.
     
    Sachin Tripathi
    Ranch Hand
    Posts: 368
    3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Correct if I am wrong
    I have been taught:
    There is a proxy class,for every array
    Which extends Objects and implements Serializable
     
    Campbell Ritchie
    Marshal
    Posts: 56584
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    What is a proxy class? I can only refer you to the JLS: link in my last post.
     
    Sachin Tripathi
    Ranch Hand
    Posts: 368
    3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Don't laugh at what my teacher responded when I asked the same question
    He said:"Do I need to tell you all what proxy actually means(he was referring to proxy attendance, about which our batch is famous for)"
    Then he said:"It is such a class,which exist just to satisfy this extension of Object class and implementation of Serializable,but don't exist in reality"
     
    Mike. J. Thompson
    Bartender
    Posts: 689
    17
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I think there is a semantic question over what exactly a class is, but the text Campbell quoted shows that Arrays are not classes. They are dynamically created types.

    Im not sure its something worth worrying about though.
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!