File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes cast int to Number Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "cast int to Number" Watch "cast int to Number" New topic

cast int to Number

Isabelle Zorbios

Joined: Apr 05, 2001
Posts: 10
Does anybody know how to cast int to Number?
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
You can't cast from int to Number because int is a primitive type and Number is an object. Casting an object is changing the reference type from one type of object to another. You first have to have an object to work with.
What you will have to do is create an instance of Integer using the constructor Integer(int myInt). Integer is a subclass of Number, so you can use it like an instance of Number or cast it, as you choose. So here is an example of how you could get an integer variable, i into a Number object numObj:
Isabelle Zorbios

Joined: Apr 05, 2001
Posts: 10
Thanks alot!
I agree. Here's the link:
subject: cast int to Number
It's not a secret anymore!