Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java基础知识-对象的实例化_Java - Fatal编程技术网

Java基础知识-对象的实例化

Java基础知识-对象的实例化,java,Java,我在学习Java时遇到了这个代码片段。对此我有一个答案,但我并不完全理解它是如何工作的 我有一个班级的课本 class Books { String author; String title; } 然后我有测试课 class TestForBook { Books[] myBooks = new Books[3]; // doubt : 1 System.out.println('myBooks length is ::' + myBooks.length);//pr

我在学习Java时遇到了这个代码片段。对此我有一个答案,但我并不完全理解它是如何工作的

我有一个班级的课本

class Books {
   String author;
   String title;
}
然后我有测试课

class TestForBook {

   Books[] myBooks = new Books[3]; // doubt : 1
   System.out.println('myBooks length is ::' + myBooks.length);//prints 3. Its correct.

  myBooks[0].title = 'Book1 title'; // This throws null pointer exception

  myBooks[0] = new Books();
  myBooks[0].title = 'Book1 title'; //works fine after above line
}

我想了解为什么即使在声明了Books类型的数组之后,数组值也有null(疑问:注释中的1,我指的是这一行)

我不知道我遗漏了Java的什么概念。以及如何/从何处学习此类主题。任何资源或书籍建议也将是可观的。谢谢


它不是问题id:1922677的副本。这里有解决方案(我也给出了解决方案),但我想知道为什么会这样。我的意思是,即使在声明之后,它为什么为null也是我的问题。

这是因为您刚刚创建了一个大小(长度)为3的空数组


在数组中定义一本书。因此,您可以在以后设置标题。

当您创建
Books[]myBooks=newbooks[3]
时。它将创建一个数组,该数组可以容纳Books的对象,但所有值都为null。您没有在该数组中放入任何内容。在访问myBooks[0]时,title实际上是在null值上调用title,因此引发null指针异常

在第二个场景中,您分配book对象,然后调用该对象的标题。 所以主要的一点是,当您对空对象调用方法或属性时,它将抛出异常。 myBooks[0]=新书();
myBooks[0]。标题='Book1 title'

创建对象数组时,请记住,
new
命令适用于数组,该数组最初包含用于容纳对象的请求插槽,但不会由任何特定对象填充。这意味着为了拥有一个包含3本书的数组,您需要

  • 创建一个可以容纳3本书的数组对象
    新书[3]
  • 创建3个图书对象
    newbook()
    ,并将它们放入数组中(每个插槽中一个)

    • 通过内联注释来理解它

      类TestForBook{

         Books[] myBooks = new Books[3]; // here you have initialized an array of size 3, but it is currently empty, but three Book object can fit in it
         System.out.println('myBooks length is ::' + myBooks.length);//prints 3. Its correct. // since you have specified the length of the array, this will return 3 despite it being empty
      
        myBooks[0].title = 'Book1 title'; // This throws null pointer exception because your books array is empty, myBooks[0] = null, hence the null pointer exception.
      
        myBooks[0] = new Books(); //now you have added a book object in the empty book array at index 0
        myBooks[0].title = 'Book1 title'; //since myBooks[0] now contains a books object that your created above, this will return that object instead of null and will work. However if you try this with myBooks[1] that will be null as you still have not put a book object at index 1
      }
      

      我想了解为什么即使在声明了Books类型的数组之后,数组值也有空值-因为在创建数组时,数组的每个元素都有该类型的默认值。类的默认值为null。您已声明了数组,但没有声明数组中的元素,因此它们有默认值,为null谢谢Edwin。明白了。
      myBooks[0] = new Books();
      
         Books[] myBooks = new Books[3]; // here you have initialized an array of size 3, but it is currently empty, but three Book object can fit in it
         System.out.println('myBooks length is ::' + myBooks.length);//prints 3. Its correct. // since you have specified the length of the array, this will return 3 despite it being empty
      
        myBooks[0].title = 'Book1 title'; // This throws null pointer exception because your books array is empty, myBooks[0] = null, hence the null pointer exception.
      
        myBooks[0] = new Books(); //now you have added a book object in the empty book array at index 0
        myBooks[0].title = 'Book1 title'; //since myBooks[0] now contains a books object that your created above, this will return that object instead of null and will work. However if you try this with myBooks[1] that will be null as you still have not put a book object at index 1
      }