![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 | |
|
Professional Programmer
Join Date: May 2006
Location: UK - London
Posts: 330
Rep Power: 3
![]() |
I am doing one of my Java assignments and I need to pass a method an int but I am receiving the data as an Object, is it possible to convert an Object to an int.
I will just demonstrate what I mean because I can't post my actual code. public void A(Object o)
{
//call B with the given data
//do something
}
public void B(int i)
{
//do something
}
__________________
Quote:
|
|
|
|
|
|
|
#2 |
|
Expert Programmer
|
Re: Object to Int
You need to know what the object is an instance of. Assuming it is an Integer, you can call the intValue() method to convert it to an int.
|
|
|
|
|
|
#3 |
|
Programming Guru
![]() ![]() ![]() |
Re: Object to Int
test
__________________
http://jasonpowers.net "There are a thousand hacking at the branches of evil to one who is striking at the root." |
|
|
|
|
|
#4 |
|
12 years old
Join Date: Nov 2007
Posts: 94
Rep Power: 1
![]() |
Re: Object to Int
Object is, haha, an object type, while int is a primitive type. You cannot translate objects to ints through direct casts; but if the object is of Integer, you could access and call the member method intValue() which returns the actual integer value being wrapped.
Example: public void A(Object o) {
if(o instanceof Integer)
B(((Integer) o).intValue());
else
throw new IllegalArgumentException();
}
public void B(int i) {
/*
code is here
*/
} |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Casting signed int to unsigned int? | null_ptr0 | C++ | 1 | Nov 23rd, 2007 9:27 PM |
| Object reference error | Jabo | Visual Basic .NET | 3 | Oct 5th, 2007 2:37 AM |
| Library problem creating Direct3d Object | Kilo | C++ | 9 | May 30th, 2006 8:48 AM |
| Pushin an object into a vector | slyadams | C++ | 18 | Mar 30th, 2006 11:17 AM |
| references passed through parameters, do they copy the object or reference the orig? | cypherkronis | Python | 1 | Jun 30th, 2005 7:38 PM |