Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 28th, 2007, 12:55 PM   #1
kruptof
Professional Programmer
 
kruptof's Avatar
 
Join Date: May 2006
Location: UK - London
Posts: 330
Rep Power: 3 kruptof is on a distinguished road
Thumbs up Object to Int

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:
When I was young it seemed that life was so wonderful,a miracle, oh it was beautiful, magical.
Now watch what you say or they'll be calling you a radical,a liberal, oh fanatical, criminal. Oh won't you sign up your name,we'd like to feel you're acceptable, respectable, oh presentable, a vegetable
kruptof is offline   Reply With Quote
Old Nov 28th, 2007, 1:56 PM   #2
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 856
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
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.
titaniumdecoy is offline   Reply With Quote
Old Nov 28th, 2007, 2:14 PM   #3
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
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."
Infinite Recursion is offline   Reply With Quote
Old Nov 28th, 2007, 4:26 PM   #4
null_ptr0
12 years old
 
Join Date: Nov 2007
Posts: 94
Rep Power: 1 null_ptr0 is on a distinguished road
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
    */
}
null_ptr0 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:02 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC