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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
flex567
Posts: 202
Joined: Mon Apr 02, 2018 8:40 am
Contact:

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

Post 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?

admin
Site Admin
Posts: 10061
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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!
If you like our products and services, please help us by posting your review here.

flex567
Posts: 202
Joined: Mon Apr 02, 2018 8:40 am
Contact:

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

Post 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);

admin
Site Admin
Posts: 10061
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

There is an explanation why it works below that!
If you like our products and services, please help us by posting your review here.

admin
Site Admin
Posts: 10061
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

flex567
Posts: 202
Joined: Mon Apr 02, 2018 8:40 am
Contact:

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

Post 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.

admin
Site Admin
Posts: 10061
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

OK, will improve.
thank you for your feedback!
If you like our products and services, please help us by posting your review here.

Username987654
Posts: 95
Joined: Sat Dec 26, 2015 6:37 pm
Contact:

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

Post 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"
?

admin
Site Admin
Posts: 10061
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

Correct. Noted.
thank you for your feedback!
If you like our products and services, please help us by posting your review here.

DazedTurtle
Posts: 26
Joined: Wed Oct 02, 2019 1:42 pm
Contact:

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

Post 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?

admin
Site Admin
Posts: 10061
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

admin
Site Admin
Posts: 10061
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

This section has now been updated as shown in this image:
test.png
test.png (90.96 KiB) Viewed 1471 times
Thank you for your feedback!
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: gadsgadsx and 119 guests