使用int,而不是字符串:公共静态无效的主要(INT []参数) [英] Using int Instead Of String: public static void main (int[] args)

查看:176
本文介绍了使用int,而不是字符串:公共静态无效的主要(INT []参数)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是IM pression下,主要的方法必须有形式的公共静态无效的主要(字串[] args){},你不可能通过INT []参数。

然而,在Windows命令行,运行以下.class文件时,它同意双方int和字符串作为参数。<​​/ P>

例如,使用此命令会给输出特约:Java的IntArgsTest桁

我的问题是,为什么呢?为什么会这样code接受一个字符串作为参数没有一个错误?

下面是我的code。

 公共类IntArgsTest
{
    公共静态无效的主要(INT []参数)
    {        IntArgsTest IAT =新IntArgsTest(参数);    }    公共IntArgsTest(INT [] n)的{的System.out.println(N [0]);};}


解决方案

进入主法的一切,所使用的JVM启动程序之一,是一个String,应有尽有。它可以像INT 1,但它确实字符串1,这是一个很大的区别。

现在您的code,如果你试图运行它时会发生什么?当然,因为它是有效的Java,但它会编译就好了你的主要方法签名不匹配由JVM需要作为节目的出发点之一。

有关您的code运行,你需要添加一个有效的主要方法一样,

 公共类IntArgsTest {
   公共静态无效的主要(INT []参数){      IntArgsTest IAT =新IntArgsTest(参数);   }   公共IntArgsTest(INT [] N){
      的System.out.println(N [0]);
   };   公共静态无效的主要(字串[] args){
      INT [] = intArgs新INT [args.length]      对于(INT I:intArgs){
         尝试{
            intArgs [I] =的Integer.parseInt(参数[I]);
         }赶上(NumberFormatException的E){
            通信System.err.println(无法试图解析一个非数字的说法,+ ARGS [I]);
         }
      }
      主(intArgs);
   }
}

再经过一些数字的时候,程序被调用。

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]);};

}

解决方案

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.

这篇关于使用int,而不是字符串:公共静态无效的主要(INT []参数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆