Assignment Conversion

Discussion in 'Java' started by Philipp, Nov 19, 2008.

  1. Philipp

    Philipp Guest

    Hello,
    In the following code the first assignment compiles OK but the second
    doesn't. I am surprised that the first is OK, but couldn't find the
    exact ref in the JLS which specifies this conversion/promotion.
    Could someone point me to the right part of the JLS? Thanks Phil

    public class ATest {
    public static void main(String[] args) {
    float f = 12.3f;
    double d = 32.1;
    f += d; // is OK
    f = f + d; // does not compile
    }
    }
    Philipp, Nov 19, 2008
    #1
    1. Advertising

  2. Philipp

    Stefan Ram Guest

    Philipp <> writes:
    >couldn't find the exact ref in the JLS which
    >specifies this conversion/promotion.


    This is given directly at the start of the
    section where one would expect it, i.e.,
    the section treating those operators.
    Stefan Ram, Nov 19, 2008
    #2
    1. Advertising

  3. Philipp

    Philipp Guest

    On Nov 19, 1:50 pm, -berlin.de (Stefan Ram) wrote:
    > Philipp <> writes:


    >   This is given directly at the start of the
    >   section where one would expect it, i.e.,
    >   the section treating those operators.


    Yeah obviously... Sorry for the noise. (I was reading CHAPTER 5
    Conversions and Promotions)
    Philipp, Nov 19, 2008
    #3
  4. Philipp

    Roedy Green Guest

    On Wed, 19 Nov 2008 04:14:43 -0800 (PST), Philipp <>
    wrote, quoted or indirectly quoted someone who said :

    > f += d; // is OK
    > f = f + d; // does not compile


    the += operators are "tough". They seem to have a built in (float) in
    them. The + operators want the explicit cast to narrow the double
    back to a float.

    Similarly you can say

    byte b;
    ..

    b++;
    b += 1;
    // but
    b = (byte)(b + 1);
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com
    Your old road is
    Rapidly agin'.
    Please get out of the new one
    If you can't lend your hand
    For the times they are a-changin'.
    Roedy Green, Nov 21, 2008
    #4
  5. Philipp wrote:
    > Hello,
    > In the following code the first assignment compiles OK but the second
    > doesn't. I am surprised that the first is OK, but couldn't find the
    > exact ref in the JLS which specifies this conversion/promotion.
    > Could someone point me to the right part of the JLS? Thanks Phil


    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.

    JLS §15.26.1 Simple Assignment Operator
    A compile-time error occurs if the type of the right-hand operand cannot
    be converted to the type of the variable by assignment conversion (§5.2).

    The common representation of the compound assignment is incorrect. |a +=
    b| is not the same as |a = a + b| but rather |a = (<type of a>)(a + b)|.
    --
    Beware of bugs in the above code; I have only proved it correct, not
    tried it. -- Donald E. Knuth
    Joshua Cranmer, Nov 21, 2008
    #5
  6. On Nov 21, 10:47 am, Roedy Green <>
    wrote:
    > On Wed, 19 Nov 2008 04:14:43 -0800 (PST), Philipp <>
    > wrote, quoted or indirectly quoted someone who said :
    >
    > >    f += d;    // is OK
    > >    f = f + d; // does not compile

    >
    > the += operators are "tough". They seem to have a built in (float) in
    > them.  The + operators want the explicit cast to narrow the double
    > back to a float.
    >
    > Similarly you can say
    >
    >      byte b;
    >      ..
    >
    >      b++;
    >      b += 1;
    >      // but
    >      b = (byte)(b + 1);


    The built-in cast in the += family of operators is mandated by the
    JLS:

    JLS 3rd Edition wrote:
    > 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. 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);


    -o
    Owen Jacobson, Nov 21, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. itsme
    Replies:
    1
    Views:
    1,622
    Ralf Hildebrandt
    Jul 23, 2003
  2. nagy
    Replies:
    36
    Views:
    996
    Terry Reedy
    Jul 20, 2006
  3. Chris
    Replies:
    34
    Views:
    1,506
  4. , India
    Replies:
    2
    Views:
    456
    Fraser Ross
    Sep 15, 2009
  5. Immortal Nephi

    Do Assignment Operator Conversion

    Immortal Nephi, Aug 27, 2010, in forum: C++
    Replies:
    4
    Views:
    255
    Alf P. Steinbach /Usenet
    Aug 27, 2010
Loading...

Share This Page