StringBuilder sb = new StringBuilder("8");
int i = 8;
System.out.println(8 + i + sb);
The correct answer given is that it does not compile. However, I have seen many instances where an object is passed into the println and there is an implicit call to it's toString(). What am I not understanding here?
When you pass an object to println, the println will internally call toString on it. However, here, the expression 8+i+sb has to be evaluated first before the result can be passed to println. The compilation error is because this expression cannot be evaluated as explained in the explanation.