Using int Instead Of String: public static void ma

2019-02-07 10:06发布

I was under the impression that the main method had to have the form "public static void main (String[] args){}", that you couldn't pass int[] arguments.

However, in windows commandline, when running the following .class file, it accepted both int and string as arguments.

For example, using this command will give the output "stringers": "java IntArgsTest stringers"

My question is, why? Why would this code accept a string as an argument without an error?

Here is my code.

public class IntArgsTest 
{
    public static void main (int[] args)
    {

        IntArgsTest iat = new IntArgsTest(args);

    }

    public IntArgsTest(int[] n){ System.out.println(n[0]);};

}

4条回答
成全新的幸福
2楼-- · 2019-02-07 10:25

Well, you can have any method with the name main with any number of arguments. But the JVM will look for the main method with the exact signature public static void main(String[]).

The main method you have defined is just another method to the class.

I don't have access to Windows now, but let me try it in a while. I did try on Fedora and of course I got the following exception:

Exception in thread "main" java.lang.NoSuchMethodError: main

Note that the class would compile fine because of the above said reason.

Update: I tested on Windows 7 and the results are same. I'm surprised how you said it worked for you.

查看更多
萌系小妹纸
3楼-- · 2019-02-07 10:25

This code will not run actually. while the code compiles (because you don't need a main to compile), when you try to run it, you will get a "Main method not found" Error.

Even better, when I ran it it said

 "please define the main method as: public static void main(String[] args)
查看更多
Fickle 薄情
4楼-- · 2019-02-07 10:40

This code contains public static void main (int[] args) which does not works. Because the JVM takes the argument values as a string argument. It does not take any int argument. So if we want a int argument means we have to convert the string argument into integer argument. For running this code valid main method is required (ex:public static void main(String args[]))

查看更多
唯我独甜
5楼-- · 2019-02-07 10:45

Everything passed into main method, the one used by the JVM to start a program, is a String, everything. It may look like the int 1, but it's really the String "1", and that's a big difference.

Now with your code, what happens if you try to run it? Sure it will compile just fine since it is valid Java, but your main method signature doesn't match the one required by the JVM as the starting point of a program.

For your code to run, you'd need to add a valid main method like,

public class IntArgsTest {
   public static void main(int[] args) {

      IntArgsTest iat = new IntArgsTest(args);

   }

   public IntArgsTest(int[] n) {
      System.out.println(n[0]);
   };

   public static void main(String[] args) {
      int[] intArgs = new int[args.length];

      for (int i : intArgs) {
         try {
            intArgs[i] = Integer.parseInt(args[i]);
         } catch (NumberFormatException e) {
            System.err.println("Failed trying to parse a non-numeric argument, " + args[i]);
         }
      }
      main(intArgs);
   }
}

And then pass some numbers in when the program is called.

查看更多
登录 后发表回答