-2

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");
    }
}
Turing85
  • 18,217
  • 7
  • 33
  • 58
Anuj Patel
  • 11
  • 1
  • 1
    The main entry point of a program usually receives `String` arguments through a command line call and `String` arguments can hold obects, maybe in JSON or XML. – deHaar Dec 12 '21 at 06:56
  • 6
    [Because the JLS says so](https://docs.oracle.com/javase/specs/jls/se8/html/jls-12.html#jls-12.1.4). – Turing85 Dec 12 '21 at 06:56
  • 2
    It harks all the way back to good old C days (and probably earlier). The only parameters which can be sent to the "main" method/function (when the program is launched) are string based, I mean, how do you send a "object" via the CLI anyway? – MadProgrammer Dec 12 '21 at 06:57
  • 1
    Does this answer your question? [Why is String\[\] args required in Java?](https://stackoverflow.com/questions/1672083/why-is-string-args-required-in-java) – Karl Knechtel Dec 12 '21 at 08:00

2 Answers2

1

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 declared public, static, and void. It must specify a formal parameter (§8.4.1) whose declared type is array of String.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
1

Because you cannot pass an object via a command line argument.

Anuj Patel
  • 11
  • 1