Skip to main content

Java += operator


Until today I thought that for example:




i += j;



is just a shortcut for:




i = i + j;



But what if we try this:




int i = 5;
long j = 8;



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



Does it mean that in fact i += j; is a shortcut for something like this i = (type of i) (i + j) ?



I've tried googling for it but couldn't find anything relevant.


Source: Tips4allCCNA FINAL EXAM

Comments

  1. As always with these questions, the JLS holds the answer. In this case §15.26.2 Compound Assignment Operators. An extract:


    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.


    And an example:


    For example, the following code is correct:

    short x = 3;
    x += 4.6;


    and results in x having the value 7 because it is equivalent to:

    short x = 3;
    x = (short)(x + 4.6);



    In other words, your assumption is correct.

    ReplyDelete
  2. A good example of this casting is using *= or /=

    byte b = 10;
    b *= 5.7;
    System.out.println(b); // prints 57


    or

    byte b = 100;
    b /= 2.5;
    System.out.println(b); // prints 40


    or

    char ch = '0';
    ch *= 1.1;
    System.out.println(ch); // prints '4'


    or

    char ch = 'A';
    ch *= 1.5;
    System.out.println(ch); // prints 'a'

    ReplyDelete
  3. Very good question. The Java Language specification confirms your suggestion.


    For example, the following code is correct:

    short x = 3;
    x += 4.6;


    and results in x having the value 7 because it is equivalent to:

    short x = 3;
    x = (short)(x + 4.6);

    ReplyDelete
  4. Yes,

    basically when we are writing

    i+=l;


    compiler is converting this to

    i = (int)(i + l);


    I just checked the .class file code.

    really a good thing to know

    ReplyDelete
  5. you need to cast from long to int explicitly in case of i = i + l then it will compile and give correct output. like

    i = i + (int)l;


    or

    i = (int)((long)i + l); // this is what happens in case of += , dont need (long) casting since upper casting is done implicitly.


    but in case of += it just works fine because the operator implicitly does the type casting from type of right variable to type of left variable so need not cast explicitly.

    ReplyDelete
  6. The problem here is of type casting.

    When you add int and long,


    The int object is casted to long & both are added and you get long object.
    but long object cannot be implicitly casted to int. So, you have to that explicitly.


    But += is coded in such a way that it does type casting. i=(int)(i+m)

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.