Hi,
I am new to JAVA. I want to know the Equivalent of " void* " in JAVA.
Kind Regards,
Abhijeet M.
Printable View
Hi,
I am new to JAVA. I want to know the Equivalent of " void* " in JAVA.
Kind Regards,
Abhijeet M.
You need to understand C or C++ to get it, and since it's been a while since I've coded with either language, I'll pass on this one.
I bet you are trying to translate a piece of C or C++ code to Java; it won't work for void pointers (void*) because you can cast them to something else in the first two language but you don't even have explicit pointers in Java and no void pointers either. There is no equivalent; a Void reference might come conceptually close but you can'nt cast it to something else. Maybe an Object reference might be better; I'd say: forget about it. The other way around: there are lots of concepts in Java that don't have a C nor C++ equivalent.
kind regards,
Jos
Yeah, I am connverting it. Now, I am using Object as an equivalent for "void *" . I didn't stuck yet by doing it. But don't know about future problems.
You're probably looking for an "everything" type, which is, as already stated in this thread, the Object type. A simple way of explaining this is probably a comparison method, like equals. You want your method to take any possible parameter, so it's reasonable to use an Object argument, like so:
First you check if the argument passed to the equals method is indeed of the MyClass type, and only then do you make the comparison.Code:public class MyClass {
private int num;
public MyClass(int num) {
this.num = num;
}
public int getNum() {
return num;
}
public boolean equals(Object o) {
if(o instanceof MyClass) {
MyClass c = (MyClass) o;
return getNum() == c.getNum();
}
return false;
}
}
Thanks moonchile for help.