【问题标题】:What is the By-Default value of a reference variable of a class which hasn't allocated any memory? [duplicate]没有分配任何内存的类的引用变量的默认值是多少? [复制]
【发布时间】:2018-02-02 13:14:05
【问题描述】:
class D 
    {
        public static void main(String[] args) 
            {
                D d;   // d is a reference variable of class D. what is its value?
                System.out.println(d);//An initializing error occurs.
            }
   }

上面给出的是一个java程序,它有一个类名D,它引用了一个引用变量d。我想知道 d 作为未初始化变量的默认值是什么

【问题讨论】:

  • 由于您的代码无法编译,因此该变量永远不会有 any 值。
  • @khelwood - 很好

标签: java


【解决方案1】:

这是一个错误,因为d 是一个局部变量,并且所有局部变量必须在它们被引用之前被初始化。如果d 是一个实例变量,那么默认值为null。 也就是说,要修复代码中的错误,您必须修复 d 的默认值。

D d = null;

【讨论】:

  • 请用无错误的代码解释一下。
  • 将代码中的D d; 替换为D d = null;。我的回答很清楚,这只是java中的规则。
  • 感谢您的澄清.....!!
猜你喜欢
  • 1970-01-01
  • 2020-11-21
  • 2019-11-24
  • 1970-01-01
  • 2011-06-25
  • 2013-01-07
  • 1970-01-01
  • 2017-02-17
相关资源
最近更新 更多