main()方法中的java args数组混淆(null检查) [英] java args array in main() method confusion (null check)

查看:148
本文介绍了main()方法中的java args数组混淆(null检查)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对某事感到困惑。我写了一个小代码片段。

I am a little confused at something. I wrote a small code snippet.

import java.util.*;
class AA
{
  public static void main(String args[])
  {
     if(args == null )
     {
          System.out.println("I am null");
     }
     else{
           System.out.println(args);
      }
 }
 }

这是一个简单的测试。虽然我没有从命令行
作为参数传递任何内容,但仍然args不为null,这意味着JVM将其初始化为
类似于String数组。为什么有这个背后的具体原因?我很好奇
知道。任何指针都会有所帮助。
谢谢,
Ben

It is a simple test. Though i am not passing anything from the command line as argument but still args is not null which means that JVM initializes it to something like String array. Why any specific reason behind this? I am curious to know. Any pointers would be helpful. Thanks, Ben

推荐答案

当你没有传递任何东西时, args 不是 null - 它为空

When you do not pass anything, the args is not null - it is empty:

 if(args.length == 0)
 {
      System.out.println("I am empty");
 } else{
       System.out.println(args);
 }

这篇关于main()方法中的java args数组混淆(null检查)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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