Which is the left-hand side in this case?The type of the variable on the left-hand side must be String otherwise the expression will not compile:
int x = 1;
x += "2";
[HD Pg 328, Sec. 12.1.1 - the-operator]
Moderator: admin
-
- Posts: 202
- Joined: Mon Apr 02, 2018 8:40 am
- Contact:
[HD Pg 328, Sec. 12.1.1 - the-operator]
-
- Site Admin
- Posts: 10061
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: [HD Pg 328, Sec. 12.1.1 - the-operator]
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!
If you like our products and services, please help us by posting your review here.
-
- Posts: 202
- Joined: Mon Apr 02, 2018 8:40 am
- Contact:
Re: [HD Pg 328, Sec. 12.1.1 - the-operator]
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);
-
- Site Admin
- Posts: 10061
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: [HD Pg 328, Sec. 12.1.1 - the-operator]
There is an explanation why it works below that!
If you like our products and services, please help us by posting your review here.
-
- Site Admin
- Posts: 10061
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: [HD Pg 328, Sec. 12.1.1 - the-operator]
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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 202
- Joined: Mon Apr 02, 2018 8:40 am
- Contact:
Re: [HD Pg 328, Sec. 12.1.1 - the-operator]
It is explained good but initially I thought it is not going to compile because on the left side is not a String.There is an explanation why it works below that!
-
- Site Admin
- Posts: 10061
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: [HD Pg 328, Sec. 12.1.1 - the-operator]
OK, will improve.
thank you for your feedback!
thank you for your feedback!
If you like our products and services, please help us by posting your review here.
-
- Posts: 95
- Joined: Sat Dec 26, 2015 6:37 pm
- Contact:
Re: [HD Pg 328, Sec. 12.1.1 - the-operator]
should beNext, the expression m += 2 will be expanded to m = m + "2"
?Next, the expression m += "2" will be expanded to m = m + "2"
-
- Site Admin
- Posts: 10061
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: [HD Pg 328, Sec. 12.1.1 - the-operator]
Correct. Noted.
thank you for your feedback!
thank you for your feedback!
If you like our products and services, please help us by posting your review here.
-
- Posts: 26
- Joined: Wed Oct 02, 2019 1:42 pm
- Contact:
Re: [HD Pg 328, Sec. 12.1.1 - the-operator]
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?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.Code: Select all
int x = 1; x += "2";
-
- Site Admin
- Posts: 10061
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: [HD Pg 328, Sec. 12.1.1 - the-operator]
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:
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.
See the sentences in the book just before the ones that you have quoted:
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.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:
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.
If you like our products and services, please help us by posting your review here.
-
- Site Admin
- Posts: 10061
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: [HD Pg 328, Sec. 12.1.1 - the-operator]
This section has now been updated as shown in this image:
Thank you for your feedback!If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: gadsgadsx and 119 guests