This question :- Make a compound assignment illegal, while simple assignment legal. Is the inverse to a question which I have already solved. Provide declarations to make x += i (compound ) legal and x = x + i (simple) not.
Which can be done by declaring x as short and i as an int. As += contains a hidden cast and x = x + i does not - leading to a compilation error.
I however have been unable to make an assignment where the compound assignment is illegal, while simple assignment is legal. Furthermore to solution to this problem to states :-
Object x = "hello "; String i = "world"; The simple assignment is legal because x + i is of type String and String is assignment compatible with Object. x = x + i; The compound assignment is illegal because the left-hand side as an object reference type other than string x += i;
However I can can compile both with no error?