why we not use Object args[] it can also hold any type of data.
class Demo {
public static void main(Object args[]) {
System.out.println("hello");
}
}
why we not use Object args[] it can also hold any type of data.
class Demo {
public static void main(Object args[]) {
System.out.println("hello");
}
}
The main method receives its arguments from the command line, which in most (all?) OSes is string oriented. In other words, the information that Java will receive to call main
, will already be in string form (and only string), so using Object[]
for main
would require programmers to add explicit casts to String
each time they would want to use an argument as a string. Using String[]
is therefor more logical, and leads to simpler code.
And of course, as Turing85 said in the comments, the legalistic point of view is that this is "because the JLS says so":
The method
main
must be declaredpublic
,static
, andvoid
. It must specify a formal parameter (§8.4.1) whose declared type is array ofString
.