Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2007

Tip: Looking for answers? Try searching our database.

How to convert an Object reference to an integer?

Thread view: 
Allen - 05 Feb 2007 06:15 GMT
I need to convert an Object reference to an integer. How to do it?
Faton Berisha - 05 Feb 2007 10:29 GMT
> 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
dagarwal82@gmail.com - 05 Feb 2007 10:32 GMT
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 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
Gordon Beaton - 05 Feb 2007 10:41 GMT
> Well, Your question is not clear but if i am getting it correctly then
> you need to override toString() method in your class.
[quoted text clipped - 10 lines]
>
> 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

Signature

[ don't email me support questions or followups ]
g o r d o n  +  n e w s  @  b a l d e r 1 3 . s e

Lew - 05 Feb 2007 14:03 GMT
> I need to convert an Object reference to an integer. How to do it?

Depends on the object. Consider
<http://java.sun.com/javase/6/docs/api/java/lang/Integer.html#parseInt(java.lang.
String
)>
with
<http://java.sun.com/javase/6/docs/api/java/lang/Object.html#toString()>

Of course, if the object is an Integer it's easier than that.

- Lew
Allen - 06 Feb 2007 01:05 GMT
> 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.
Patricia Shanahan - 06 Feb 2007 06:22 GMT
>> I need to convert an Object reference to an integer. How to do it?
>
[quoted text clipped - 3 lines]
> 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
John Maline - 06 Feb 2007 18:44 GMT
>> I need to convert an Object reference to an integer. How to do it?
>
> 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
Allen - 07 Feb 2007 01:08 GMT
> >> I need to convert an Object reference to an integer. How to do it?
>
[quoted text clipped - 29 lines]
> 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.
jmaline@gmail.com - 07 Feb 2007 02:22 GMT
> It is a requirement by JNI.
> I package object values to a byte buffer. As we know, C++ has address
[quoted text clipped - 15 lines]
> 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
Esmond Pitt - 07 Feb 2007 02:59 GMT
> It is a requirement by JNI.
> I package object values to a byte buffer. As we know, C++ has address
[quoted text clipped - 15 lines]
> 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.
Jeff Higgins - 07 Feb 2007 03:05 GMT
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]
srinivas.veeranki@gmail.com - 06 Feb 2007 11:57 GMT
> 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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.