什么是“String [] args”包含在java中? [英] What does "String[] args" contain in java?

查看:120
本文介绍了什么是“String [] args”包含在java中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行以下程序时:

public class Test
{
    public static void main(String[] args)
    {
        System.out.println(args);
    }
{

打印: [Ljava .lang.String; @ 153c375

当我再次运行它时,会打印: [Ljava.lang。字符串; @ 1d1e730

and when I run it again, it prints: [Ljava.lang.String;@1d1e730

每次给我不同的输出

所以,什么 [Ljava.lang.String; @ 153c375 是什么意思?

So, what does "[Ljava.lang.String;@153c375" mean?

推荐答案

更新:我刚刚意识到我从来没有回答过问题什么字符串[] args包含在java中? :-)这是一个命令数组提供给程序的-line参数,每个参数在数组中是 String

Update: I just realized I never answered the question "What does "String[] args" contain in java?" :-) It's an array of the command-line arguments provided to the program, each argument being a String in the array.

我们现在继续我们定期安排的答案...

And we now resume with our regularly-scheduled answer...

args 是一个数组。要查看单个命令行参数,请索引数组 — args [0] args [1] 等:

args is an array. To see individual command-line arguments, index into the array — args[0], args[1], etc.:

你可以像这样遍历args:

You can loop through the args like this:

public class Test
{
    public static void main(String[] args)
    {
        int index;

        for (index = 0; index < args.length; ++index)
        {
            System.out.println("args[" + index + "]: " + args[index]);
        }
    }
}

java测试一两三,即输出:

args[0]: one
args[1]: two
args[2]: three

如果你不需要索引那么循环:

Or loop like this if you don't need the index:

public class Test
{
    public static void main(String[] args)
    {
        for (String s : args)
        {
            System.out.println(s);
        }
    }
}




那么,[Ljava.lang.String; @ 153c375是什么意思?

这是Java的默认 toString String [] 的返回值(的数组>串)。请参阅 对象#的toString [表示数组, L 表示类或接口, java .lang.String 是不言自明的。该部分来自 类#的getName() ; @ 153c375 ; @ 后跟 hashCode 数组的十六进制字符串。 (我认为 hashCode 的默认实现 Object 表示数组所在的内存位置,这就是为什么它不同对于您的程序的不同调用,但这是未指定的行为,无论如何都不会对您有任何用处。)

That's Java's default toString return value for String[] (an array of String). See Object#toString. The [ means "array", the L means "class or interface", and java.lang.String is self-explanatory. That part comes from Class#getName(). The ;@153c375 is ;@ followed by the hashCode of the array as a hex string. (I think the default implementation of hashCode for Object indicates where in memory the array is located, which is why it's different for different invocations of your program, but that's unspecified behavior and wouldn't be any use to you anyway.)

这篇关于什么是“String [] args”包含在java中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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