Page 1 of 1

[HD Pg 328, Sec. 12.1.1 - the-operator]

Posted: Fri May 10, 2019 1:47 am
by flex567
The type of the variable on the left-hand side must be String otherwise the expression will not compile:
int x = 1;
x += "2";
Which is the left-hand side in this case?

Re: [HD Pg 328, Sec. 12.1.1 - the-operator]

Posted: Fri May 10, 2019 2:27 am
by admin
Not sure what is the confusion here. Left hand side is always the same i.e. on the left of the operator. Operator is +=. So, the left side is x!

Re: [HD Pg 328, Sec. 12.1.1 - the-operator]

Posted: Fri May 10, 2019 2:40 am
by flex567
Aha but then in the next example we don't have a String on the left side:
Object m = 1;
m += "2";
System.out.println(m);

Re: [HD Pg 328, Sec. 12.1.1 - the-operator]

Posted: Fri May 10, 2019 2:44 am
by admin
There is an explanation why it works below that!

Re: [HD Pg 328, Sec. 12.1.1 - the-operator]

Posted: Fri May 10, 2019 2:49 am
by admin
You have to understand that it is a book not a specification. You cannot take every statement and expect it to be complete on its own. There will be exceptions, there will be special cases and so on. They will all be discussed as needed. You have to read the whole thing to get the complete picture. If you want to read text where every statement will stand on its own, you might want to read the Java Language Specification.

Re: [HD Pg 328, Sec. 12.1.1 - the-operator]

Posted: Fri May 10, 2019 3:16 am
by flex567
There is an explanation why it works below that!
It is explained good but initially I thought it is not going to compile because on the left side is not a String.

Re: [HD Pg 328, Sec. 12.1.1 - the-operator]

Posted: Fri May 10, 2019 9:37 am
by admin
OK, will improve.
thank you for your feedback!

Re: [HD Pg 328, Sec. 12.1.1 - the-operator]

Posted: Thu Sep 05, 2019 10:18 am
by Username987654
Next, the expression m += 2 will be expanded to m = m + "2"
should be
Next, the expression m += "2" will be expanded to m = m + "2"
?

Re: [HD Pg 328, Sec. 12.1.1 - the-operator]

Posted: Thu Sep 05, 2019 11:40 pm
by admin
Correct. Noted.
thank you for your feedback!

Re: [HD Pg 328, Sec. 12.1.1 - the-operator]

Posted: Fri Oct 04, 2019 5:56 pm
by DazedTurtle

Code: Select all

int x = 1;
x += "2";
The above code will not compile because you can't assign an object of type String to a variable of type int. The type of the right operand can be anything because if the operand is not a string, it will be converted to a string as per the rules discussed above.
Regarding the part I bolded, if the right operand were not a string, why would it be converted to one when you said in the previous sentence that a string on the right hand side would prevent the code from compiling due to the left operand being an int?

Re: [HD Pg 328, Sec. 12.1.1 - the-operator]

Posted: Fri Oct 04, 2019 6:34 pm
by admin
It is talking about the case where type on the left hand side is a string.
See the sentences in the book just before the ones that you have quoted:
If the type of the
variable on the left is String, it performs a string concatenation. Here is an example:
String s = "1";
s += 2; //expanded to s = s + 2;
System.out.println(s); //prints "12"
The type of the variable on the left-hand side must be String or something that can refer to a
String (there are only two such types really - CharSequence and Object), otherwise the expression
will not compile:
If the type on the left hand side is not a string but the right hand is a string , it will fail compilation as explained in the text that you have quoted. The concatenation operator will convert 1 + "2" into a string containing "12" but it won't be able to assign it back to x because x is an int.

When type of left hand is a string, the type of the right hand of += doesn't matter. It can be anything because it will be converted to a string.

Re: [HD Pg 328, Sec. 12.1.1 - the-operator]

Posted: Fri Oct 04, 2019 11:26 pm
by admin
This section has now been updated as shown in this image:
test.png
test.png (90.96 KiB) Viewed 1855 times
Thank you for your feedback!