Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7514585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:32:24+00:00 2026-05-30T00:32:24+00:00

I spotted Java's +=, -=, *=, /= compound assignment operators (good question :)), but

  • 0

I spotted Java's +=, -=, *=, /= compound assignment operators (good question :)), but it had a part that I don’t quite understand. Borrowing from that question:

int i = 5;
long l = 8;

Then i = i + l; will not compile but i += l; will compile fine.

The accepted answer to the linked question states that:

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.

which gives that i += l; is the same as i = (int)((i) + (l)); with the exception that i is only evaluated once.

A long may be (IIRC even is guaranteed to be) longer than an int, and thus can hold a much greater range of values.

Given that this situation can very easily cause data loss due to necessary narrowing conversion at some point during execution of the statement (either r-value expression evaluation, or assignment), why is i += l; not a compile-time error or at least warning?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T00:32:26+00:00Added an answer on May 30, 2026 at 12:32 am

    Given that this situation can very easily cause data loss due to necessary narrowing conversion at some point during execution of the statement (either r-value expression evaluation, or assignment), why is i += l; not a compile-time error or at least warning?

    It probably should be, as you said, either a compile-time error or at least warning. Most books and tutorials that I’m aware of introduce x += y; as shorthand for x = x + y;. I honestly don’t know of any that make the distinction called out in section 15.26.2 Compound Assignment Operators of the JLS except one.

    In chapter 2 (puzzle 9) of Java Puzzlers: Traps, Pitfalls, and Corner Cases the authors (Joshua Bloch & Neal Gafter) ask you to provide declarations for x and i such that this is a legal statement:

    x += i;
    

    and this is not:

    x = x + i;
    

    There are lots of solutions, including the first two lines of code that you posted in your question. The authors warn against using compound assignment operators on variables of types byte, short, and char, and recommend that when using these operators on variables of type int you should make sure that the RHS expression is not a long, float, or double.

    They conclude with the following observation (emphasis mine):

    In summary, compound assignment operators silently generate a cast. If the type of the result of the computation is wider than that of the variable, the generated cast is a dangerous narrowing cast. Such casts can silently discard precision or magnitude. For language designers, it is probably a mistake for compound assignment operators to generate invisible casts; compound assignments where the variable has a narrower type than the result of the computation should probably be illegal.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've spotted this question: Does Microsoft SkyDrive have an API? But not having an
I have spotted an example in book: iOS4 Programming Cookbook that I can't understand:
just curious! but I spotted that the value of π held by SAS is
I have just spotted that something is wrong with my django-admin.py command. I checked
Firstly, I realise that this is a very similar question to this one: Which
I have spotted a method in some code that does the following : def
I have recently spotted that it is possible in iOS to use OpenAL to
In our JUnit testsuite, I have spotted a few tests that do not drive
I think I know the answer to this one, but I have just spotted
I have a bash script that uses ssh to execute commands remotely. I spotted

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.