Casting Object to Integer

If I have a method like :public void DoX(Object o)Why can't I do :Integer x = (Integer o); or just (if o instance of Integer) ?Thanks.
[177 byte] By [JF_La] at [2007-10-3 4:28:47]
# 1
Integer x = (Integer) o;
cotton.ma at 2007-7-14 22:31:46 > top of Java-index,Java Essentials,Java Programming...
# 2
Yeah, of course it was a typo in the first message.The error I have is Eclipse telling me that Object can't be cast to Integer.I'm using Java 1.5 and Eclipse 3.1
JF_La at 2007-7-14 22:31:46 > top of Java-index,Java Essentials,Java Programming...
# 3
do object.getClass() and make sure it's an Integer
SoulTech2012a at 2007-7-14 22:31:46 > top of Java-index,Java Essentials,Java Programming...
# 4

> Yeah, of course it was a typo in the first message.

>

Well how would I know this?

> The error I have is Eclipse telling me that Object

> can't be cast to Integer.

>

Eclipse is lying. Or you have another typo you haven't shown. Or are doing something else.

public class Test{

public static void main(String[] args){

Object o = new Integer(42);

Integer i = (Integer) o;

}

}

Compiles just fine.

cotton.ma at 2007-7-14 22:31:46 > top of Java-index,Java Essentials,Java Programming...
# 5
Sorry guys, Note to self : Never create a class named Object again or you'll make a fool of yourself.
JF_La at 2007-7-14 22:31:46 > top of Java-index,Java Essentials,Java Programming...
# 6
> Sorry guys, > > Note to self : Never create a class named Object> again or you'll make a fool of yourself.That's one of the better ways to learn.
aniseeda at 2007-7-14 22:31:46 > top of Java-index,Java Essentials,Java Programming...