Using int() on an Integer is a Bad Thing

Apologies in advance if it's just me having a bad brain day, but I'm getting some rather odd results with this sketch:

    void setup()
    {
      Integer myInteger = 0x23456789;

      println("myInteger in hex=" + hex(myInteger)); // Just using hex()
      println("myHexFromCast =" + hex((int)myInteger, 8)); // hex of an  int cast
      println("myHexFromMethod =" + hex(int(myInteger), 8)); // hex of the int() method
    }

Yeah, I realize (now) that int(myInteger) is wrong given that int() is just for primitive data types, but I would have thought that something somewhere would have yelped in pain that I even tried to do it.

The output I get is: myInteger in hex=23456789 myHexFromCast =23456789 myHexFromMethod =23456780

So no error, but a subtly wrongo result.

If you change the initial value of myInteger to 0x2345FFFF you get:

    myInteger in hex=2345FFFF
    myHexFromCast =2345FFFF
    myHexFromMethod =23460000

It has the hallmarks of either some weird conversion, some kind of binary format issue, with a tad of a rounding error thrown in (rounding for integers?) Or it's just my addled brain with a parity error.

Anyone know what's going on, please?

Andy. :)

Answers

  • edited April 2017

    @ajohnsonlaird -- I believe that you don't need to cast Integer to int due to autounboxing. Also keep in mind that you can println(Integer) directly because the method is overloaded.

    All of these things work:

    Integer myInteger = new Integer(0x23456789);
    println(myInteger);
    println(myInteger + " ");
    println(Integer.parseInt(myInteger.toString()));
    println(hex(myInteger, 8));
    

    The problem you are experiencing with int(Integer i) has nothing to do with hex specification or hex() conversion -- that just complicates the examples. Likewise, (int) casting is fine. Using the Processing function int() on an Integer is the problem:

    Integer myInteger = new Integer(0x23456789);
    println(myInteger);      // good
    println((int)myInteger); // good
    println(int(myInteger)); // bad
    myInteger = new Integer( 591751049);
    println(myInteger);      // good
    println((int)myInteger); // good
    println(int(myInteger)); // bad
    myInteger = new Integer(-591751049);
    println(myInteger);      // good
    println((int)myInteger); // good
    println(int(myInteger)); // bad
    

    It is possible that a bug (or documentation caveat) should be filed against Processing int()... maybe someone has more insight into why this might be expected? For more on Java Integer -> int in general beyond Processing, see:

  • edited April 2017 Answer ✓

    Integer class has the method Integer.intValue() that returns an int with the value of the Integer.

  • Thanks for the responses. I only tripped over this problem because of my ineptitude but I was surprised that it (using int() on an Integer) didn't provoke an error, but just returned an incorrect value.

    Thanks LOTG for the tip re: that intValue() method....

    Andy.

Sign In or Register to comment.