Click to See Complete Forum and Search --> : What's equivalent of VARIANT in Java?


July 13th, 2000, 09:18 PM
Hi,

I am new to Java. But i have worked on VC and VB.

I wanted to know what is equivalent in of VARIANT (in VC/VB) in Java.

My requirement is something like this. I have an array of values each of which
is of a different data type. i need to convert the contents of each of them
into the appropriate datatype and store in an object X.

Could someone tell me what 'X' should look like? In VC/VB its fine since i could have a array of VARIANT's and for each element i can set it types and store the data. how would one do it i Java

Thanks in advance,
Priti.

Eugen SEER
July 14th, 2000, 10:43 AM
Hi,
I would say that the root class of all objects, namely Object is some kind of equivalent for VARIANT.

Hoang Truong
July 15th, 2000, 04:49 PM
Hi there,

Object class is the parent of all classes in Java.
The following simple program would show you how to work with different data types.


import java.util.Vector;

public class Objects {
public Vector objects = null;

public Objects() {
objects = new Vector();
}

public void addObject(Object o) {
objects.addElement(o);
}

public void list() {
Object element = null;
for (int i=0; i<objects.size(); i++) {
element = objects.elementAt(i);
if (element instanceof Integer)
System.out.println(((Integer)element).toString() + " : integer");
else if (element instanceof Double)
System.out.println(((Double)element).toString() + " : double");
else if (element instanceof Float)
System.out.println(((Float)element).toString() + " : float");
else if (element instanceof String)
System.out.println(element + " : string");
// and more ...
}
}

public static void main(String args[]) {
Objects objects = new Objects();
objects.addObject(new String("My name"));
objects.addObject(new Double(12.232345454));
objects.addObject(new Integer(45));
objects.addObject(new Float(34.34));
objects.list();
}
}

Good luck.
Hoang.