![]() | #3.en Object, reference, equality, nullity, mutability |
var book = new Book();
System.out.println(book.title + ' ' + book.author);
Describe the result.
var b1 = new Book("Da Java Code", "Duke Brown");
var b2 = b1;
var b3 = new Book("Da Java Code", "Duke Brown");
System.out.println(b1 == b2);
System.out.println(b1 == b3);
What does the above code display?
public static void main(String[] args){
var b1 = new Book("Da Java Code", "Duke Brown");
var b2 = b1;
var b3 = new Book("Da Java Code", "Duke Brown");
var list = new ArrayList();
list.add(b1);
System.out.println(list.indexOf(b2));
System.out.println(list.indexOf(b3));
}
What is wrong with the results displayed on the console?
var aBook = new Book(null, null);
var anotherBook = new Book(null, null);
var list = new ArrayList();
list.add(aBook);
System.out.println(list.indexOf(anotherBook));
Where is the issue?
var book = new Book("Da Java Code", "Dan Duke");
System.out.println(book);
Java can do this, as long as you put in the class
Book a public String toString () method
(the definition of this method is
in the class java.lang.Object (RTFM!))
returning a character string, which we typically construct
from the attributes of the object.
Da Java Code by Dan Duke