Object conversion

HiI need to store integers or characters into an Object array.How can i convert integers or characters into objects and vice versa.Thanks
[165 byte] By [drrpbsa] at [2007-10-3 2:16:10]
# 1

Hope this sample will help you

Integer a = new Integer("2");

Objects obj [] = new Object[10];

obj [0] = a;

obj [1] = "testing";

int extract = ((Integer)obj[0]).intValue();

String temp = (String)obj[1];

just_a_kid83a at 2007-7-14 19:15:02 > top of Java-index,Java Essentials,New To Java...
# 2
Thank you..How can i do the reverse.I'm doing something like this.Object stack[] = new Object[];int num = 10;stack[0] = num;But its giving error.How can i convert integers to ObjectsThanks
drrpbsa at 2007-7-14 19:15:02 > top of Java-index,Java Essentials,New To Java...
# 3
And the wrapper for a single char is the Character class: http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Character.html
prometheuzza at 2007-7-14 19:15:02 > top of Java-index,Java Essentials,New To Java...
# 4
> But its giving error.> How can i convert integers to Objects?just_a_kid83 gave an example how to do just that!
prometheuzza at 2007-7-14 19:15:02 > top of Java-index,Java Essentials,New To Java...
# 5

Try this

Object stack[] = new Object[];

int num = 10;

Integer numWrapper = new Integer(num);

stack[0] = numWrapper;

//to extract

int extract = ((Integer)stack[0]).intValue();

just_a_kid83a at 2007-7-14 19:15:02 > top of Java-index,Java Essentials,New To Java...
# 6

> Try this

>

> Object stack[] = new Object[];

> int num = 10;

>

> Integer numWrapper = new Integer(num);

>

> stack[0] = numWrapper;

>

> //to extract

> int extract = ((Integer)stack[0]).intValue();

That will throw a runtime exception.

prometheuzza at 2007-7-14 19:15:02 > top of Java-index,Java Essentials,New To Java...
# 7
In fact, it won't even compile. The initialization of the array should specify the length.Other than that, it will run just fine.
Herko_ter_Horsta at 2007-7-14 19:15:02 > top of Java-index,Java Essentials,New To Java...
# 8
> In fact, it won't even compile. The initialization of> the array should specify the length.> > Other than that, it will run just fine.Ah yes, of course!; )
prometheuzza at 2007-7-14 19:15:02 > top of Java-index,Java Essentials,New To Java...