Consider the below scenario:
String title;
public Dummy_3(String title) {
this.title = title;
}
public String getTitle() {
return title;
}
In the main method, even if I use clone() or =, the results are the same. Can someone explain?.
Using =:
public static void main(String[] args) {
Dummy_3 obj1 = new Dummy_3("Avengers: Infinity War");
Dummy_3 obj2 = new Dummy_3("Avengers: Endgame");
System.out.println(obj1.getTitle());
System.out.println(obj2.getTitle());
obj1 = obj2;
System.out.println(obj1.getTitle());
System.out.println(obj2.getTitle());
}
Using clone():
public static void main(String[] args) {
Dummy_3 obj1 = new Dummy_3("Batman Begins");
Dummy_3 obj2 = new Dummy_3("Batman: The Dark Knight");
System.out.println(obj1.getTitle());
System.out.println(obj2.getTitle());
obj2 = obj1.clone();
System.out.println(obj1.getTitle());
System.out.println(obj2.getTitle());
}
Note: I know that there is a duplicate question, but the answer is not clear for me. Appreciate it if anyone can explain. Question