Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Casting from Object to int

1,045 views
Skip to first unread message

gaztedo

unread,
Dec 20, 2007, 6:44:08 AM12/20/07
to
Hi there!

I'm developing a method which receives two Object parameters, and I need
to cast one of them to int. How can I do it?

public method(Object a, Object b){
this.b = (int)b; // doesn't work!
this.a = a;
}

Thanks in advance!

Sabine Dinis Blochberger

unread,
Dec 20, 2007, 7:07:04 AM12/20/07
to
gaztedo wrote:

First of all, casting just means "lets treat this as if it were type x".
Why can't you require a parameter of type Integer or even int?
--
Sabine Dinis Blochberger

Op3racional
www.op3racional.eu

Sabine Dinis Blochberger

unread,
Dec 20, 2007, 7:12:09 AM12/20/07
to
gaztedo wrote:

Please don't multipost. Read the FAQ, it was just posted by David Alex
Lamb.

Lew

unread,
Dec 20, 2007, 8:37:54 AM12/20/07
to
gaztedo wrote:
>> I'm developing a method which receives two Object parameters, and I need
>> to cast one of them to int. How can I do it?
>>
>> public method(Object a, Object b){
>> this.b = (int)b; // doesn't work!
>> this.a = a;
>> }

Sabine Dinis Blochberger wrote:
> Please don't multipost. Read the FAQ, it was just posted by David Alex
> Lamb.

In the first place, gaztedo, 'int' is not an object type, so no Object can
ever be cast to 'int'. In the second place, there's no way to guarantee to a
public method that its argument values are of any particular subtype or value,
or conversely, that they aren't. These conditions therefore have to be
checked inside the method. Third, Sabine has given you a good idea about
letting the method signature determine the type as narrowly as you need.

Finally, although you named your code snippet 'method', you gave it the syntax
of 'constructor'. You must provide a return type for a method, e.g.,
public void foo( int a, int b ) { ... }

--
Lew

Matt Wharton

unread,
Dec 20, 2007, 7:38:06 PM12/20/07
to
"gaztedo" <du...@mail.com> wrote in message
news:476a558a$1...@filemon1.isp.telecable.es...

Refer to the advice others have given about good OO practices, etc (i.e.
what you are trying to do here probably is NOT the best way to go about
things). Having said that, you might try the following. If you are on Java
1.5 or higher, you can take advantage of auto-boxing/unboxing.

public void method(Object a, Object b)
{
if (b instanceof Integer)
{
this.b = (Integer)b; // Cast to Integer, not int. Will auto-unbox the
Integer as an int and store to this.b.
}

this.a = a;
}

Cheers,

-Matt

Vitvitskiy Vladimir

unread,
Dec 21, 2007, 3:57:24 AM12/21/07
to
you can use this

public static void method(Object a, Object b) {
int bb = (Integer)b;
int aa = (Integer)a;
}

"gaztedo" <du...@mail.com> wrote in message
news:476a558a$1...@filemon1.isp.telecable.es...

Roedy Green

unread,
Dec 21, 2007, 2:54:17 PM12/21/07
to
On Fri, 21 Dec 2007 10:57:24 +0200, "Vitvitskiy Vladimir"
<vvitv...@ukrsotsbank.com> wrote, quoted or indirectly quoted
someone who said :

> public static void method(Object a, Object b) {
> int bb = (Integer)b;
> int aa = (Integer)a;

In JDK 1.5+ which has autounboxing.

In older Java you need to unbox manually.

int bb = ((Integer)b).intValue();
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

iola...@lakeheadu.ca

unread,
Apr 24, 2013, 4:47:54 PM4/24/13
to

iola...@lakeheadu.ca

unread,
Apr 24, 2013, 4:50:51 PM4/24/13
to
For you to cast from object to int, i would advice you to use this method which is as follow if the Integer.ParseInt(object.toSring())
The string representation of the object is returned by the toString() and the string return value is passed as an arguement to the Integer.ParseInt() method.
Good luck, should you need further clarification, do let me know

Lew

unread,
Apr 25, 2013, 4:19:21 PM4/25/13
to
iola...@lakeheadu.ca wrote:
>iola...@lakeheadu.ca wrote:
>> On Thursday, December 20, 2007 3:44:08 PM UTC+4, gaztedo wrote:
>>> I'm developing a method which receives two Object parameters, and I need
>>> to cast one of them to int. How can I do it?
>>>
>>> public method(Object a, Object b){
>>> this.b = (int)b; // doesn't work!
>>> this.a = a;
>>> }
>
> For you to cast from object to int, i [sic] would advice you to use this method which is as follow
> if the Integer.ParseInt(object.toSring()) [sic]

But of course you have to catch any 'NumberFormatException', and it's really bad design, and
why are you answering a five-year-old thread?

> The string representation of the object is returned by the toString() and the string return value is
> passed as an arguement to the Integer.ParseInt() method.
> Good luck, should you need further clarification, do let me know

Unlikely the OP is still checking on this thread, wouldn't you think?

And they might want to check with someone who gives better advice anyway.

Like, declare the method to take an 'int' in the first place.

--
Lew

0 new messages