How to convert an Object reference to an integer?

F

Faton Berisha

I need to convert an Object reference to an integer. How to do it?

As it is, the question is very unclear.
What properties of the object should the resulting integer reflect?

F. Berisha
 
D

dagarwal82

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??
 
G

Gordon Beaton

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??

I don't claim to know what the OP is actually asking for, but if your
interpretation is the correct one, I fail to see the logic in
returning the integer as a String that needs to be parsed.

If you are in control of the object code to begin with, return the
value directly, from a suitably named method:

public int toSomeNumber() {
return someNumber;
}

/gordon
 
A

Allen

I need to convert an Object reference to an integer. How to do it?


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.
 
P

Patricia Shanahan

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.

It MAY give you a unique key. It cannot always guarantee that. In
particular, a 64 bit JVM on a large system may have more objects than
there are distinct values of int.

Why is it better/easier to remember the int than the object reference?

Patricia
 
S

srinivas.veeranki

I need to convert an Object reference to an integer. How to do it?

Hi,
You can use the following statement to convert from Object to integer.

Integer.parseInt(String.valueOf(object));

Here 'object' must contain integer reference value then only it will
works otherwise it throws NumberFormatException.
 
J

John Maline

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
 
A

Allen

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.
 
J

jmaline

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.

Holy cow, you're talking JNI, not just Java. That's a completely
different thing! You're not getting much help because you're not
asking your question very well.

How about starting over. Something like... "I want to use JNI
function X() to accomplish Y. I'm having trouble understanding how
to format the data for parameter Z." Fill in those blanks and a bit
of explanation and you've got a much better chance of getting help.

Better still, go to http://www.google.com/codesearch and type in the
JNI function you're trying to use. You'll probably come up with 100
examples of code using it. Browse a few examples and you might find
one that helps you out.

I'm afraid my JNI is rusty enough that I probably won't be any more
help.

Good luck,
John
 
E

Esmond Pitt

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.

No but C does. Pass the reference to the JNI function *as a reference*
(i.e. a jobject?), then convert it to int or long in there. You also
have to do something to lock the address while you're using it but it's
nearly ten years since I did this and I can't remember the details.
 
J

Jeff Higgins

Allen wrote:
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.

[http://www.javolution.org/api/javolution/io/Struct.html]
 

Ask a Question

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.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top