1 2 3 Previous Next 68 Replies Latest reply: Dec 15, 2007 11:58 PM by 807603 RSS

    Java gotcha

    807603
      I've discussed this on another forum, but when I first ran into this in my code, it really confused me as to what was going on. So I figured some of the posters here might enjoy this:
      Object o = true ? new Integer(1) : new Double(2.0);
      What is the result and why?
        • 1. Re: Java gotcha
          807603
          Object o = true

          = is not ==
          • 2. Re: Java gotcha
            807603
            TuringPest wrote:
            Object o = true

            = is not ==
            "=" has lower precedence than "?:"
            • 3. Re: Java gotcha
              807603
              What? Does the ternary operator (unintuitively) have lower precedence than assignment or something?
              • 4. Re: Java gotcha
                807603
                paulcw wrote:
                What? Does the ternary operator (unintuitively) have lower precedence than assignment or something?
                No. Try o.toString() and see what happens.
                • 5. Re: Java gotcha
                  807603
                  if i had to hazard a guess:
                  Object o = new Integer(1);
                  1) execute the ternary (true?new Integer:new Double)
                  2) assign results to Object o


                  Where have i gone wrong?

                  Edited by: redfalconf35 on Dec 14, 2007 3:41 PM
                  • 6. Re: Java gotcha
                    807603
                    I'm giving you the easy version. When I first ran into it, my code was more like this:
                    Object o = true ? new Integer(0) : new Double(0.0);
                    Now THAT gives a weird result.
                    • 7. Re: Java gotcha
                      807603
                      redfalconf35 wrote:
                      if i had to hazard a guess:
                      Object o = new Integer(1);
                      1) execute the ternary (true?new Integer:new Double)
                      2) assign results to Object o


                      Where have i gone wrong?

                      Edited by: redfalconf35 on Dec 14, 2007 3:41 PM
                      Run it and see the result of o.toString()
                      • 8. Re: Java gotcha
                        807603
                        Oh. Interesting.

                        For those who don't want to compile: The compiler sees that the ternary operator is returning either an Int or a Double, and it casts the int to a double.
                        • 9. Re: Java gotcha
                          807603
                          Look at the byte codes
                             0:   new     #2; //class java/lang/Integer
                             3:   dup
                             4:   iconst_1
                             5:   invokespecial   #3; //Method java/lang/Integer."<init>":(I)V
                             8:   invokevirtual   #4; //Method java/lang/Integer.intValue:()I
                             11:  i2d
                             12:  invokestatic    #5; //Method java/lang/Double.valueOf:(D)Ljava/lang/Double;
                             15:  astore_1
                             16:  getstatic       #6; //Field java/lang/System.out:Ljava/io/PrintStream;
                             19:  aload_1
                             20:  invokevirtual   #7; //Method java/io/PrintStream.println:(Ljava/lang/Object;)V
                             23:  return
                          Amusing.
                          • 10. Re: Java gotcha
                            807603
                                Object o = true ? new Integer(0) : new Double(0.0);
                                System.out.println("class=" + o.getClass().getName() + ", o=" + o.toString());
                            
                                o = true ? new Integer(1) : new Double(2.0);
                                System.out.println("class=" + o.getClass().getName() + ", o=" + o.toString());
                            
                            Output:
                            class=java.lang.Double, o=0.0
                            class=java.lang.Double, o=1.0
                            When I first ran into the problem, my code was like the first condition, so it really looked like the boolean part of the ternary operator was backwards, but I was absolutely sure it wasn't.
                            • 11. Re: Java gotcha
                              807603
                              paulcw wrote:
                              Oh. Interesting.

                              For those who don't want to compile: The compiler sees that the ternary operator is returning either an Int or a Double, and it casts the int to a double.
                              I've never liked autoboxing/unboxing, and this really made me hate it :-)

                              It unboxes the Integer, promotes it to a double, then reboxes it to put it in the Object.
                              • 12. Re: Java gotcha
                                807603
                                The compiler decides on the type of the expression "true ? new Integer(1) : new Double(2.0)" as Double. Binary conversion, unboxing and value set conversion may all be involved according to JLS 15.25.
                                • 13. Re: Java gotcha
                                  807603
                                  pbrockway2 wrote:
                                  The compiler decides on the type of the expression "true ? new Integer(1) : new Double(2.0)" as Double. Binary conversion, unboxing and value set conversion may all be involved according to JLS 15.25.
                                  Right on.
                                  • 14. Re: Java gotcha
                                    796254
                                    who'd write code like that anyway? looks heinous to me.

                                    %
                                    1 2 3 Previous Next