.equals() vs == ================== public class Main { public static void main(String[] args) { String s1 = "red"; String s2 = ""; s2 += "r"; s2 += "e"; s2 += "d"; System.out.println(s1.equals("red")); // return true System.out.println(s2.equals("red")); // return true System.out.println(s1 == "red"); // return ____ ^ System.out.println(s2 == "red"); // return ____ } } /* It is wrong to compare string contents with "==" Reason: "==" tests whether the LHS and RHS refer to the same object but not compare the contents. (Ref. Topic02 Page 9). But for "why ^ returns true?" It is because JVM makes smart optimization for string literals ("String Interning" - https://www.baeldung.com/java-string-pool) ** Similar thing happens in other compiled languages, e.g. C++. ^_^ */ this and super ============== class A { public String tellMe() {return "a";} public void alg() {System.out.println(tellMe() + "*");} } class B extends A { public String tellMe() {return "b";} public void alg() {System.out.println(tellMe() + "#");} } class C extends A { public String tellMe() {return "c";} } public class Main { public static void main(String [] args) { A x1 = new B(); x1.alg(); //b# B x2 = new B(); x2.alg(); //b# A x0 = new A(); x0.alg(); //______________ A x3 = new C(); x3.alg(); //______________ C x4 = new C(); x4.alg(); //______________ } } /* b# b# a* c* c* */ super, Object, toString ============================== class X { // the super class is Object public void alg() { System.out.println(super.toString()); //like: X@28a418fc System.out.println(toString()); //like: X@28a418fc // Why the same? // Answer: // Similar to Employee and Manager: // Inside Manager.java, // we can type: super.getName() // Or simply type: getName(); } } public class Main { public static void main(String[] args) { X x = new X(); x.alg(); } } this, super, Object, toString ============================== class A { public String toString() {return "a";} public void alg() {System.out.println(super.toString() + "*");} } class B extends A { public String toString() {return "b";} public void alg() {System.out.println(super.toString() + "#");} } class C extends A { public String toString() {return "c";} } public class Main { public static void main(String [] args) { A x1 = new B(); x1.alg(); //b# B x2 = new B(); x2.alg(); //b# A x0 = new A(); x0.alg(); //______________ A x3 = new C(); x3.alg(); //______________ C x4 = new C(); x4.alg(); //______________ } } /* a# a# A@2a139a55* C@368239c8* C@9e89d68* */ Dimension needed? ================== import java.util.*; public class Main { public static void main(String [] args) { ArrayList arr1 = new ArrayList<>(); //Compilation error //ArrayList arr2 = new ArrayList<>(); //OK } }