Explicitly and without apology a marketing vehicle MaryMaryQuiteContrary

Monday Aug 09, 2004

Please join me in congratulating Kris Schneider, who correctly answered last week's puzzler and is therefore the proud owner of our fabulous package of prizes.

Lars chimed in with a hallelujah from the amen chorus so he gets some free stuff too!

So you're wondering, how'd these guys get so smart?

Hint: pick C

So just to make sure you're thorougly infotained (Informed + Entertained = Infotained. don't tell anybody British though. i think they get annoyed when you make up words), we're going to reprint without permission (don't tell Betsy) this highly informative and hilarious exchange which i got using my free 30-min sampler of JavaOne Online and i'm giving to you using my very favorite marketing technique -- copy, paste.

Josh: Welcome back to Code Talk. Yesterday I gave you a Puzzler in which you were asked to provide definitions for x and i so that:

   x += i;     // (1)

is a legal statement, but

   x = x + i;  // (2)

is not.

Neal: Isn't statement (1) just a shorthand for statement (2)?

Josh: Not quite. Statement (2) is an ordinary assignment; statement (1) uses +=, which is a compound assignment operator. If you look at JLS 15.26.2, you'll see that compound assignments automatically cast the expression on their right-hand side to the type of the expression on their left-hand side. That's what's known as an implicit narrowing cast.

Neal: Aha! So if I were to declare:

    short x = 0;
    int   i = 1;

the first statement would be legal, but the second would be illegal because it's attempting to assign an int to a short.

Josh: Exactly.

Neal: But isn't that dangerous?

Josh: Yep; it can lead to silent loss of precision. For example, you might expect this program to print out 123456:

    short x = 0;
    x += 123456;
    System.out.println(x);

You'd be wrong. It prints out -7616. To avoid this sort of rude surprise, don't use compound assignment operators (such as +=, -=, and *=) on variables of type byte byte, short, char, or float.

Neal: Sound advice. Now on to my puzzler. You'll recall that I asked you to provide definitions for x and i so that:

   x = x + i; // (3)

is a legal statement, but

   x += i;    // (4)

is not.

Josh: Yep; the exact opposite of my puzzler. It was hard!

Neal: You really can't solve it without knowing the gory details of the assignment operators. It turns out that all compound assignment operators require both operands to be of primitive type with one exception: += allows the right-hand operand to be of any type if the left-hand operand is of type String (JLS 15.26.2). The simple assignment operator (=) is much more general: you can use object reference types to your heart's content so long as the expression on the right-hand side is assignable to the variable on the left-hand side (JLS 5.2).

Josh: Aha. So if I were to declare:

    Object x = "Buy ";
    String i = "Effective Java!";

statement (3) would be legal, because the left-hand side is a variable of type Object and the right-hand side is a expression of type String. Statement (4) would be illegal because the left-hand side has an object reference type other than String.

Neal: Yep. Simple as that.

Josh: Is there a moral?

Neal: Don't code like my brother.

Josh: Don't code like my brother.

that's all for today.

mary

p.s. I met an angel in disguise today and his name is Eric Reed. Thanks for everything Eric.

p.p.s. Calvin and Blogs rocks. Bookmark. Shift, reload.

 

Comments:

Post a Comment:
Comments are closed for this entry.