This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
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
Author

cast int to Number

Isabelle Zorbios
Greenhorn

Joined: Apr 05, 2001
Posts: 10
Hi!
Does anybody know how to cast int to Number?
isabelle
Anonymous
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
Greenhorn

Joined: Apr 05, 2001
Posts: 10
Ok...
Thanks alot!
 
GeeCON Prague 2014
 
subject: cast int to Number