• Post Reply Bookmark Topic Watch Topic
  • New Topic

casting int to Integer  RSS feed

 
Gabriel White
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there, been kicking around a program that will dump random numbers into an array. I have the numbers working correctly and I have the array's converted into objects rather than primitive types due to the return in the method. I am just wondering why I can't cast my primitive type of num as an Integer? Or maybe I am just doing it incorreclty.

Thanks in advance.


[ January 23, 2006: Message edited by: Gabriel White ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Java 5, you can do that assignment, actually, via "autoboxing".: The compiler will automatically change it into something like this:

a[i] = new Integer(num);

Of course, in earlier Java versions, you can do this manually.
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prior to 1.5, autoboxing and unboxing is not done. You would have to explicitly turn an int into an Integer to store it in an array that holds Integers.
 
Gabriel White
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks E.

That did it. I guess I should log into my java.sun account more often and check on the news.



Thanks again.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:
In Java 5, you can do that assignment, actually, via "autoboxing".: The compiler will automatically change it into something like this:

a[i] = new Integer(num);

Of course, in earlier Java versions, you can do this manually.


As a note, a "new" instance of Integer is not guaranteed when boxed, unless 'num' is outside some specific bounds: -128 to 127.

Related fun: http://jqa.tmorris.net/GetQAndA.action?qids=7
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Beginners, you may safely disregard this post; I'm discussing minutae again. But for those who care:

[Tony]: As a note, a "new" instance of Integer is not guaranteed when boxed, unless 'num' is outside some specific bounds: -128 to 127.

Rather, there is no guarantee either way, outside those bounds. Within the bounds, it's guaranteed that you'll get the same result for any two boxing conversions of the same number. So

    (Integer) 1 == (Integer) 1

is always true, but

    (Integer) 256 == (Integer) 256

may or may not be true. On Sun's current JDK 5, it's false (as Tony already knows). For those who are curious, the code responsible for this can be seen in Integer.valueOf():


See JLS3 5.1.7, in particular:

----

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.

This ensures that in most common cases, the behavior will be the desired one, without imposing an undue performance penalty, especially on small devices. Less memory-limited implementations might, for example, cache all characters and shorts, as well as integers and longs in the range of -32K - +32K.

----

By the way Tony, I haven't forgotten the previous thread about switch statements. I was busy for a bit, then you were gone, but now that we're both here again I'll get back to that shortly. Sorry for the delay. I'm sure you've been on the edge of your seat waiting.
[ January 23, 2006: Message edited by: Jim Yingst ]
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you even using an array of Integers? You can easily create an array of int instead and avoid all this confusion. The Integer class is mostly intended for use where you need to have an Object instead of a primitive, such as with the Collections API. In this case, I don't see any compeling reason to use Objects instead of primitives.

Layne
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!