Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2007

Tip: Looking for answers? Try searching our database.

Cast from Object to int?

Thread view: 
gaztedo - 20 Dec 2007 11:43 GMT
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!
Thomas Kellerer - 20 Dec 2007 11:48 GMT
gaztedo, 20.12.2007 12:43:
> Hi there!
>
[quoted text clipped - 5 lines]
>     this.a = a;
> }

I guess the fact that your method declaration is invalid syntax is a
typing error in the email.

You cannot cast an Object to an int. If b is e.g. a Number object (e.g.
Integer or Long) you can do the following:

this.b = ((Number)b).intValue()

Btw: why don't you simple define your method as
public void method(Object o, int value)

Thomas
gaztedo - 20 Dec 2007 12:29 GMT
> You cannot cast an Object to an int. If b is e.g. a Number object (e.g.
> Integer or Long) you can do the following:
>
> this.b = ((Number)b).intValue()

Thanks! It worked nice!

> Btw: why don't you simple define your method as
> public void method(Object o, int value)

I cannot do it... it's a long story ;)
Andrew Thompson - 20 Dec 2007 14:16 GMT
...
>> Btw: why don't you simple define your method as
>> public void method(Object o, int value)
>
>I cannot do it... it's a long story ;)

Fortunately this is a big discussion forum, and
a lot of people enjoy a long story.

Signature

Andrew Thompson
http://www.physci.org/

gaztedo - 20 Dec 2007 21:26 GMT
>> I cannot do it... it's a long story ;)
>
> Fortunately this is a big discussion forum, and
> a lot of people enjoy a long story.

Well, not such a huge story. I'm developing a parser using Yacc, and it
requires to develop some constructors (before I said methods, sorry)
using Object as a type.

So that was the reason I had to use Object and I couldn't use Integer.

BTW I still have a lot of different errors :S
Martin Gregorie - 21 Dec 2007 17:13 GMT
>>> I cannot do it... it's a long story ;)
>>
[quoted text clipped - 4 lines]
> requires to develop some constructors (before I said methods, sorry)
> using Object as a type.

Why use yacc with Java?

Have you looked at some of the Java equivalents, such as Coco/R?
http://www.ssw.uni-linz.ac.at/Research/Projects/Coco/

Signature

martin@   | Martin Gregorie
gregorie. | Essex, UK
org       |

Roedy Green - 20 Dec 2007 18:33 GMT
>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?
You can't because int is not an Object.  You could cast it perhaps to
Integer or to whatever the object actually was. From there you can
convert, but not cast, it to int.

See http://mindprod.com/jgloss/conversion.html

To find out what kind of animal you have:

System.out.println( obj.getClass().toString() );
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.