Reputation: 4414
In java, as per the Java Language Specification 12.1.4 the main method should be declared as follows:
public static void main(String[] args){
//code of program
}
With the use of String[] args
we can get command-line arguments. Sometime those command-line arguments needs to be converted to appropriate type. suppose if you have supplied any number 123
as an argument and you want to convert it to int
then we can use one of the following conversion methods:
int n1=Integer.valueOf(args[0]);
//or
int n2=Integer.parseInt(args[0]);
But, if java could have provided use of Objects[] args
instead String[] args
then we can easily convert from one type to another, for this example conversion would take place as follows:
public static void main(Object[] args){
int n1=(Integer)args[0];
}
I'm not getting this why java have used String[] args
and not Object[] args
?
Upvotes: 2
Views: 2748
Reputation: 23361
This is simple because the args
came from outside java environment, it is from the Operating System and as such it can't create Java Objects it can only pass Strings
To call a java program from a console it would be something like:
c:\java MyProgram 123
There is no way to java know that the OS is passing an object it is just a string.
Upvotes: 3
Reputation: 300
May be because "String" is an array of char sequence which takes continuous memory, and which can be entered using standard keyboard. Otherwise how main method will handle type casting.
Upvotes: 1
Reputation: 726579
Java is designed with portability in mind, so that Java programs would be invoked by diverse operating environments. Having the environments pass Java objects to main
would put too much restrictions on the caller system, because that system would have a task of "understanding" the input and converting it to Java objects.
Strings, on the other hand, are universal: all operating environments have them. Therefore, Java specification required strings, knowing that your Java program would be able to process and understand them better than an external environment.
Upvotes: 7
Reputation: 201447
Because the operating system doesn't pass variable typed arguments to the JVM. If you had Object[] args
the JVM couldn't really infer their types like that without the Operating System. It's also the general contract (in Java of course) of "standard" (e.g. ancestral C/C++) Main function.
Upvotes: 1
Reputation: 23443
public static void main(String[] args)
is a main method that runs when you execute the Class file $java MyClass input1 input2
.
Upvotes: 1
Reputation: 14699
Because you cannot pass an object via a command line argument. Instead, it's natural to interpret the passed arguments as Strings
.
Upvotes: 1
Reputation: 26209
whenever you read from Console
it is by default String
that is the reason they have made it as String[] array
.
if you use :
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter String");
String str = br.readLine();
String variable str
by contains String because br.ReadLine()
gives you string
Upvotes: 2