A
Allen
I need to convert an Object reference to an integer. How to do it?
I need to convert an Object reference to an integer. How to do it?
Well, Your question is not clear but if i am getting it correctly then
you need to override toString() method in your class.
public String toString()
{
// return something that has an integer..
}
now if you have something like :-
MyClass c = new MyClass
then do the following :-
Integer.parseInt(c);
Is that You wanted??
Allen said:I need to convert an Object reference to an integer. How to do it?
I need to convert an Object reference to an integer. How to do it?
Allen said:Sorry. I did not make it clear.
I want to convert an Object reference to an integer. And save the
integer. When use the object again, I look up the reference by that
integer key. Now I know System.identityHashCode(object) can give me an
unique integer.
I need to convert an Object reference to an integer. How to do it?
Allen said:Now I know System.identityHashCode(object) can give me an
unique integer.
Read the javadoc more closely. There's no guarantee that the output of
System.identityHashCode() is unique. Two separate objects might give
the same integer result. If you need guaranteed unique integers, you're
out of luck.
Here's a quote from the version 1.5 javadoc for Object.hash() which is
referenced by System.identityHashCode(). Highlighting is mine.
**As much as is reasonably practical**, the hashCode method defined
by class Object does return distinct integers for distinct objects.
(This is typically implemented by converting the internal address
of the object into an integer, but this implementation technique
is not required by the JavaTM programming language.)
What are you doing that you can store an integer key but you can't store
an object reference? If it's to interface with an external system,
could you use a string instead? That way you could (in some
application-dependent way) define a string that maps uniquely to the
object. Use a HashMap with the string as the key and object as the
value. Or for that matter, if you can define (in some
application-dependent way) an integer that maps uniquely to the object,
use that instead of a String. But it would have to be
application-dependent based on the properties of the object or other
application-specific logic. I don't think you'll find a
guaranteed-unique, system-generated integer to map to an object.
Regards,
John
It is a requirement by JNI.
I package object values to a byte buffer. As we know, C++ has address
and can convert to an integer.
I cannot package Object reference to a byte buffer, so I need convert
it to an unique integer. To illustrate it,
see below.
|-----------------------|
| Type |
------------------------
| Length |
------------------------
| Address |
-------------------------
JNI takes the third element as an address, i.e. an unique integer, I
hope to package the Java object reference.
Because the object reference is like an address. But Java does not
permit converting a reference to an integer.
Allen said:It is a requirement by JNI.
I package object values to a byte buffer. As we know, C++ has address
and can convert to an integer.
I cannot package Object reference to a byte buffer, so I need convert
it to an unique integer. To illustrate it,
see below.
|-----------------------|
| Type |
------------------------
| Length |
------------------------
| Address |
-------------------------
JNI takes the third element as an address, i.e. an unique integer, I
hope to package the Java object reference.
Because the object reference is like an address. But Java does not
permit converting a reference to an integer.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.