29

Possible Duplicate:
Java += operator

In Java, this is not valid (doesn't compile), as expected:

long lng = 0xffffffffffffL;
int i;
i = 5 + lng;    //"error: possible loss of magnitude"

But this is perfectly fine (?!)

long lng = 0xffffffffffffL;
int i = 5;
i += lng;       //compiles just fine

This is obviously a narrowing operation, that can possibly exceed the int range. So why doesn't the compiler complain?

Community
  • 1
  • 1
Cristian Diaconescu
  • 34,633
  • 32
  • 143
  • 233

3 Answers3

14

This is defined in the JLS #15.26.2:

A compound assignment expression of the form E1 op= E2 is equivalent to E1 = (T) ((E1) op (E2)), where T is the type of E1, except that E1 is evaluated only once.

In other words, i += lng performs a cast implicitly.

assylias
  • 321,522
  • 82
  • 660
  • 783
  • not jelous, but i said the same darn thing in my answer a minute befor you and i got no reps.. :P – PermGenError Dec 19 '12 at 10:13
  • 2
    @GanGnaMStYleOverFlowErroR You just rephrased OP's question. assylias got upvoted for referencing the JLS. As opposed to you, he posted that in the **first** revision, and was the first one to post it; you needed about 5 revisions to get there (and only after seeing others' answers). – Marko Topolnik Dec 19 '12 at 10:14
  • @GanGnaMStYleOverFlowErroR Sorry about that - but I'm certainly not going to give away the 3 hats I just got ;-) – assylias Dec 19 '12 at 10:15
  • dammit, is it all about the hats now?? :) – Marko Topolnik Dec 19 '12 at 10:16
  • @MarkoTopolnik Yes - reputation is way overstated. Only hats count. – assylias Dec 19 '12 at 10:17
  • @MarkoTopolnik i did reference JLS in my answer too .. but whatever, i am not really greedy abouts reps or hats :P – PermGenError Dec 19 '12 at 10:19
  • @assylias haha, why would you say sorry .. i was just makin a conversation thats all.. +1 anyways to have more reps than the question which is already asked :) – PermGenError Dec 19 '12 at 10:19
  • @GanGnaMStYleOverFlowErroR ...in your first revision of the answer, or in sixth? I know which, I've been watching your edits. – Marko Topolnik Dec 19 '12 at 10:21
  • @MarkoTopolnik hahah, yeah it was later on i referenced the JLS, but still my answer still reflects what's said in JLS doesn't it ?? lemme know if there's something wrong in my answer :) – PermGenError Dec 19 '12 at 10:24
  • @GanGnaMStYleOverFlowErroR If you want to know why you didn't get the upvotes, it is because your answer was sitting voteless at the moment assylias posted his; everybody upvoted the clean answer backed with a JLS reference and from that point on it was irrelevant to everybody that there happened to be other zero-score answers below that said exactly the same thing. Learn from this and become a better contributor to StackOverflow. **Especially** because soon you'll be a *k-guy*. – Marko Topolnik Dec 19 '12 at 10:27
  • 1
    @MarkoTopolnik mark as said earlier, i am not greedy or something. i totally agree with you that my answer wasn't as clear as assylias or NPE, i was only making a conversation :p, and i would love to be a better cobntributor to SO .. :) – PermGenError Dec 19 '12 at 10:30
5

i += lng; compound assignment operator cast's implicitly.

i+=lng; 
is same as 
i = int(i+lng);

FROM JLS:

A compound assignment expression of the form E1 op= E2 is equivalent to E1 = (T) ((E1) op (E2)), where T is the type of E1, except that E1 is evaluated only once.

PermGenError
  • 45,977
  • 8
  • 87
  • 106
  • 1
    So it seems, but why? It seems to me to go against the gist of the language. – Cristian Diaconescu Dec 19 '12 at 10:06
  • @CristiDiaconescu, This implicit cast happens without errors because there is no syntax available to make it explicit. For example you wouldn't be able to `*=1.5` an integer otherwise. – Robert Nov 13 '14 at 20:19
3

The compiler does not complain because, according to JLS §15.26.2. Compound Assignment Operators:

A compound assignment expression of the form E1 op= E2 is equivalent to E1 = (T) ((E1) op (E2)), where T is the type of E1, except that E1 is evaluated only once.

Thus,

i += lng;

is equivalent to

i = (int)(i + lng);
NPE
  • 486,780
  • 108
  • 951
  • 1,012