Discussion:
Casting from Object to int
(too old to reply)
gaztedo
2007-12-20 11:44:08 UTC
Permalink
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
2007-12-20 12:07:04 UTC
Permalink
Post by gaztedo
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!
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
2007-12-20 12:12:09 UTC
Permalink
Post by gaztedo
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!
Please don't multipost. Read the FAQ, it was just posted by David Alex
Lamb.
--
Sabine Dinis Blochberger

Op3racional
www.op3racional.eu
Lew
2007-12-20 13:37:54 UTC
Permalink
Post by Sabine Dinis Blochberger
Post by gaztedo
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;
}
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
2007-12-21 00:38:06 UTC
Permalink
Post by gaztedo
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!
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
2007-12-21 08:57:24 UTC
Permalink
you can use this

public static void method(Object a, Object b) {
int bb = (Integer)b;
int aa = (Integer)a;
}
Post by gaztedo
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!
Roedy Green
2007-12-21 19:54:17 UTC
Permalink
On Fri, 21 Dec 2007 10:57:24 +0200, "Vitvitskiy Vladimir"
Post by Vitvitskiy Vladimir
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
i***@lakeheadu.ca
2013-04-24 20:47:54 UTC
Permalink
Post by gaztedo
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!
i***@lakeheadu.ca
2013-04-24 20:50:51 UTC
Permalink
Post by gaztedo
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!
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
2013-04-25 20:19:21 UTC
Permalink
Post by gaztedo
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
Loading...