So everybody thinks this is normal?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    So everybody thinks this is normal?

    I bumped my reply from another thread just because I don't believe that everybody
    thinks this is normal. Have a close read:

    I don't like autoboxing very much; below you'll see an example derived from an
    original example by someone else posted in Sun's Java forum quite a while ago:

    [Code=java]
    import java.util.*;

    public class Autoboxing {

    public static void main(String[] args) {

    Object foo = new Long(0xcafebabe deadbeefL);
    List<Object> bars = new ArrayList<Objec t>();

    bars.add(bool() ? (Long)foo : (Number)foo);
    bars.add(bool() ? (Long)foo : (Long)foo);
    bars.add(bool() ? (Long)foo : (Double)foo);
    bars.add(bool() ? (Long)foo : ((Long)foo).lon gValue());

    System.out.prin t("== :");
    for (Object bar : bars)
    System.out.prin t(" "+(foo == bar));
    System.out.prin tln();

    System.out.prin t("equals:");
    for (Object bar : bars)
    System.out.prin t(" "+foo.equals(ba r));
    System.out.prin tln();
    }

    private static boolean bool() {
    return true;
    }
    }[/code]


    Output:

    Code:
    ==    : true true false false
    equals: true true false true
    That's why I don't like autoboxing very much ...

    kind regards,

    Jos
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Is that what they were talking about in

    Originally posted by JLS
    Ideally, boxing a given primitive value p, would always yield an identical reference. In practice, this may not be feasible using existing implementation techniques. The rules above are a pragmatic compromise. The final clause above requires that certain common values always be boxed into indistinguishab le objects. The implementation may cache these, lazily or eagerly.

    For other values, this formulation disallows any assumptions about the identity of the boxed values on the programmer's part. This would allow (but not require) sharing of some or all of these references.

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by r035198x
      Is that what they were talking about in
      Not really; it's the ternary operator that spoils all that nice hulla baloo:

      Originally posted by JLS
      The type of a conditional expression is determined as follows:


      If the second and third operands have the same type (which may be the null type), then that is the type of the conditional expression.
      If one of the second and third operands is of type boolean and the type of the other is of type Boolean, then the type of the conditional expression is boolean.
      If one of the second and third operands is of the null type and the type of the other is a reference type, then the type of the conditional expression is that reference type.
      Otherwise, if the second and third operands have types that are convertible (§5.1.8) to numeric types, then there are several cases:
      If one of the operands is of type byte or Byte and the other is of type short or Short, then the type of the conditional expression is short.
      If one of the operands is of type T where T is byte, short, or char, and the other operand is a constant expression of type int whose value is representable in type T, then the type of the conditional expression is T.
      If one of the operands is of type Byte and the other operand is a constant expression of type int whose value is representable in type byte, then the type of the conditional expression is byte.
      If one of the operands is of type Short and the other operand is a constant expression of type int whose value is representable in type short, then the type of the conditional expression is short.
      If one of the operands is of type; Character and the other operand is a constant expression of type int whose value is representable in type char, then the type of the conditional expression is char.
      Otherwise, binary numeric promotion (§5.6.2) is applied to the operand types, and the type of the conditional expression is the promoted type of the second and third operands. Note that binary numeric promotion performs unboxing conversion (§5.1.8) and value set conversion (§5.1.13).
      Otherwise, the second and third operands are of types S1 and S2 respectively. Let T1 be the type that results from applying boxing conversion to S1, and let T2 be the type that results from applying boxing conversion to S2. The type of the conditional expression is the result of applying capture conversion (§5.1.10) to lub(T1, T2) (§15.12.2.7).
      At run time, the first operand expression of the conditional expression is evaluated first; if necessary, unboxing conversion is performed on the result; the resulting boolean value is then used to choose either the second or the third operand expression:

      If the value of the first operand is true, then the second operand expression is chosen.
      If the value of the first operand is false, then the third operand expression is chosen.
      The chosen operand expression is then evaluated and the resulting value is converted to the type of the conditional expression as determined by the rules stated above. This conversion may include boxing (§5.1.7) or unboxing conversion. The operand expression not chosen is not evaluated for that particular evaluation of the conditional expression
      A Long isn't a Long when one of the other operands happens to be a Double etc.

      funny eh?

      Jos

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by JosAH
        Not really; it's the ternary operator that spoils all that nice hulla baloo:



        A Long isn't a Long when one of the other operands happens to be a Double etc.

        funny eh?

        Jos
        At least they wrote it down.
        Determining the type of an expression was always going to involve some voodoo somewhere.

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by r035198x
          At least they wrote it down.
          Determining the type of an expression was always going to involve some voodoo somewhere.
          Not in C or C++ and not in Java before that darn autoboxing that is. But I guess
          most of the people take it all for granted until something goes wrong ;-)

          kind regards,

          Jos

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by JosAH
            Not in C or C++ and not in Java before that darn autoboxing that is. But I guess
            most of the people take it all for granted until something goes wrong ;-)

            kind regards,

            Jos
            What? You expect them all to read (whatever that word means) that JLS?

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by r035198x
              What? You expect them all to read (whatever that word means) that JLS?
              Mwah, not me; you're the optimist of the group here ;-)

              kind regards,

              Jos

              Comment

              Working...