-2

I have a basic question regarding java.I know it's very basic.However, I want to confirm whether my approach is correct or not.

Generally,we write

public static void main(String[] args)

The compiler starts executing from there on!

I tried it writing in a different way.

class input
 {
 public static void main(String args)
  {

   input.main("hello");
   System.out.println(args);
}
}

The error i am getting is:

 Exception in thread "main" java.lang.NoSuchMethodException:   
 substrings.main([Ljava.lang.String;)
 at java.lang.Class.getMethod(Class.java:1786)
 at com.intellij.rt.execution.application.AppMain.main(AppMain.java:125)

I have two questions:

What is wrong in having String args instead of String[] args??(Is JVM hardcoded like accepting array arguments?)

What's wrong if i call it as input.main("0") ?

Roman C
  • 49,761
  • 33
  • 66
  • 176
mohan babu
  • 1,388
  • 2
  • 17
  • 35
  • Your question is like asking why car can't run on olive oil. It simply was designed to run on petrol oil and petrol oil only. You could have your car run on olive oil by going through a lots of hoops, but in the end the effort would not be worth the result and everyone would just stroke their head why did you waste so much on effort on something so useless. – The Law Oct 18 '15 at 17:53
  • You don"t call the main method from inside your programm. this method is called from 'outside' and the array args are the command line arguments you pass to it – Tobi Oct 18 '15 at 17:53
  • Google for some information on how to input string arguments.. – Ramesh-X Oct 18 '15 at 17:54
  • 2
    The JVM is hardcoded yes. And there's nothing wrong with having a String args method too, if you want. – NickJ Oct 18 '15 at 17:55
  • The JVM constructs an array of String from the given command line arguments and call the main method using the same. So yes it is hard coded. As for why is it not a String, because any number of command line arguments(containing any character) can be supplied to a program – shanmuga Oct 18 '15 at 18:00
  • If needed you can write your own JVM launcher to call some method other than main(String[]). But it might not be worth the troubles. – shanmuga Oct 18 '15 at 18:08

1 Answers1

1

Your main function MUST match the function signature in the main function specification.

Ling Zhong
  • 1,744
  • 14
  • 24