The java.util.Date class overrides the equals method to return true if two objec
ID: 3828646 • Letter: T
Question
The java.util.Date class overrides the equals method to return true if two objects have the same date and time. Show the output of the following code.
import java.util.*;
public class Test extends Object {
public static void main(String[] args) {
Date d1 = new Date();
Date d2 = new Date(349324);
Date d3 = d1;
System.out.println("(1) " + (d1 == d2));
System.out.println("(2) " + (d1 == d3));
System.out.println("(3) " + d1.equals(d2));
System.out.println("(4) " + d1.equals(d3));
}
}
Explanation / Answer
import java.util.*;
public class Test extends Object {
public static void main(String[] args) {
//Here we are creating a Date class Object
Date d1 = new Date();
//Here we are creating a Date class Object
Date d2 = new Date(349324);
//Assigning the reference d1 to the the variable d3 of type Date.
Date d3 = d1;
//Here we are comparing the memory locations of the Date d1 and d2
System.out.println("(1) " + (d1 == d2));
//Here we are comparing the memory locations of the Date d1 and d3
System.out.println("(2) " + (d1 == d3));
//Here we are comparing the contents inside the Date class objects d1 and d2
System.out.println("(3) " + d1.equals(d2));
//Here we are comparing the contents inside the Date class objects d1 and d3
System.out.println("(4) " + d1.equals(d3));
}
}
________________
Output:
(1) false
(2) true
(3) false
(4) true
________________
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.