What is the result of the following?
public class Main{ public static void main(String[] argv){ List<String> one = new ArrayList<String>(); one.add("abc"); List<String> two = new ArrayList<>(); two.add("abc"); if (one == two) System.out.println("A"); else if (one.equals(two)) System.out.println("B"); else System.out.println("C"); } }
B.
one == two is false since the variables do not point to the same object.
one.equals(two) is true since it compares ArrayList for same elements in the same order. It is re-implemented for ArrayList.
public class Main{ public static void main(String[] argv){ List<String> one = new ArrayList<String>(); one.add("abc"); //from w w w. ja v a2 s .c o m List<String> two = new ArrayList<>(); two.add("abc"); if (one == two) System.out.println("A"); else if (one.equals(two)) System.out.println("B"); else System.out.println("C"); } }
The code above generates the following result.