How do you declare x and y so that x+=y gives a compilation error and x=x+y not?

JavaOperatorsShorthand

Java Problem Overview


I ran into this question in an interview and couldn't come up with a solution. I know the vice versa can be done as shown in https://stackoverflow.com/questions/7456462/what-does-the-operator-do-in-java

So the question was like below.

..... x = .....;
..... y = .....;

x += y; //compile error
x = x + y; //works properly

Java Solutions


Solution 1 - Java

Try this code

Object x = 1;
String y = "";

x += y; //compile error
x = x + y; //works properly

not entirely sure why this works, but the compiler says

> The operator += is undefined for the argument type(s) Object, String

and I assume that for the second line, toString is called on the Object.

EDIT:

It makes sense as the += operator is meaningless on a general Object. In my example I cast an int to an Object, but it only depends on x being of type Object:

Object x = new Object();

It only works if x is Object though, so I actually think it is more that String is a direct subclass of Object. This will fail for x + y:

Foo x = new Foo();

for other types that I have tried.

Solution 2 - Java

It is not possible.

X x = ...;
Y y = ...;

x += y;         //1
//equivalent to
x = (X) (x+y);  //2

x = x+y;        //3

Suppose the type of x+y is Z. #2 requires a casting conversion from Z to X; #3 requires an assignment conversion from Z to X. "casting conversions are more inclusive than assignment conversions"(1). Therefore, as long as #3 is legal, #2 is legal, and #1 is legal.

On the reverse side, it is possible that #1 is legal, but #3 is illegal, for example

    byte x = 0;
    int y  = 1;
    x+=y;     // ok, x=(byte)(x+y), cast int to byte is allowed.
    x = x+y;  // error, assign int to byte

This information is not useful whatsoever; it is a flaw of Java making such surprising differences.

(1) http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#5.5

Solution 3 - Java

int  i = 5;
String s = "a";
System.out.println(i+=s);  //Error
System.out.println(i+s);   // No error

Basically, works for Any object or any non-string primitive and String combination.

I wonder which company it was? :)

Solution 4 - Java

This thing will not always give you compilation error

If you are doing smoething like this :

class A{
public static void main(String args[]){
	String x = "10";
	String y = "s";
	x += y;
	System.out.println(x);
}
}

It will work fine

even if you do

class A{
public static void main(String args[]){
	int x = 10;
	float y = 11.5F;
	x += y;
	System.out.println(x);
}
}

it will work properly.

But if you take x and y two different type of variables like :

class X{
 }
class A{
public static void main(String args[]){
	X x = new X();
	float y = 11.5F;
	x += y;
	System.out.println(x);
}
}

In such cases it will fail to compile.

*Even you can concatinate any int, float etc with String.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionknshnView Question on Stackoverflow
Solution 1 - JavaskynetView Answer on Stackoverflow
Solution 2 - JavairreputableView Answer on Stackoverflow
Solution 3 - JavaBhushanView Answer on Stackoverflow
Solution 4 - JavagprathourView Answer on Stackoverflow