Hello and happy new year folks.
I am new at JAVA. I have written a code as below. When I run it I get following output.
1 and 2 are same
1 and 2 are equal
Why can't I get same output for the other if statements? All 3 objects (example1, example2 and example3) is derived from class 'Test'. All 3 are from same origin, class 'Test'. So not only 1vs 2 but also 1 vs 3 and 2 vs 3 should be same/equal.
Any idea please.?
MY CODE IS HERE:
public class Test {
public static void main(String[] args) {
Test example1 = new Test();
Test example2 = example1;
Test example3=new Test();
if (example1==example2){
System.out.println("1 and 2 are same");
}
if (example1==example3){
System.out.println("1 and 3 are same");
}
if (example2==example3){
System.out.println("2 and 3 are same");
}
if (example1.equals(example2)){
System.out.println("1 and 2 are equal");
}
if (example1.equals(example3)){
System.out.println("1 and 3 are equal");
}
if (example2.equals(example3)){
System.out.println("2 and 3 are equal");
}
}
}