This week's book giveaway is in the OCP forum. We're giving away four copies of OCP Oracle Certified Professional Java SE 11 Developer Practice Tests and have Scott Selikoff and Jeanne Boyarsky on-line! See this thread for details.
Originally posted by Barry Gaunt: Actually it prints:
which is interesting because it implies that it is first converted to an array and then java.util.Arrays.toString is used to print it.
If the argument is null, then the result is "null". If the argument implements Formattable, then its formatTo method is invoked. Otherwise, the result is obtained by invoking the argument's toString() method.
So AbstracCollection's toString method is invoked.
Explaination. You are passing list and list1 both reference the same ArrayList, so you can add a String to list1, but you wouldn't be able to add a String to list. But since list is still pointing to the same ArrayList it will have the second added String in the ArrayList and print it.