Sign in to follow this  
ProperFuked

[java] comparing variables of object class in java

Recommended Posts

ProperFuked    118
Hi, i need some help: in my program i have a class Object variable, which stores some input (could be int, or string or whatever). However - if the input is an integer, i have to be able to compare it to another variable, also of type object (which stores some other int value). I'm trying to find information on casting from object into int, however i'm not having much luck. so i guess what i need is a simple method or procedure, which could cast an Obj variable into an int, so that i can compare them. Oh another thing, i have to be able to compare to see if one is less than or greater than, not just to see if they are equal (that would be easy, i know). Thank you ahead of time for any help.

Share this post


Link to post
Share on other sites
Lucidquiet    199
Is this homework?

Objects are not primitives, and in java there is no "casting" from an Object to a primitive be it 'int' or 'boolean' or 'char'. What you might need to do however is cast it to some class that has a proper 'get' integer method so that you can compare that integer to some other integer. This in effect allows you to use the methods found in the Class to which you are casting said Object.

If you are trying to compare Object to Object's take a look at the 'Comparator' interface in the API since that will also let you define some behavior for less than, etc. You can also override the method equals(Object o) found in the Object class, in which cas you will have to cast to some appropriate class in order to do the comparison between 'this' class and the one represented as 'o'.

L-

Share this post


Link to post
Share on other sites
H_o_p_s    877
I think you could also do something like this:
if(getSomeRandomObject().getClass().getName().equals("Whatever name your looking for"))
//do what ever

I think… I'm not sure…

Share this post


Link to post
Share on other sites
Whackjack    100
Quote:
Original post by H_o_p_s
I think you could also do something like this:
if(getSomeRandomObject().getClass().getName().equals("Whatever name your looking for"))
//do what ever

I think… I'm not sure…


If you're gonna do that, you might as well do

if(object instanceof Foo)
// do whatever

Much faster than trying to do a String comparison (and simpler).

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Quote:
Original post by pinacolada
You should also check out the Integer class, it's the standard way of passing ints as objects.


In 1.5 there's no need because it has autoboxing and unboxing. Stuff like this is legal:

[source="java"]
LinkedList<Integer> list = new LinkedList<Integer>();
list.add(10);
int i=list.get(0);

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this