How to convert an Object reference to an integer?

Discussion in 'Java' started by Allen, Feb 5, 2007.

  1. Allen

    Allen Guest

    I need to convert an Object reference to an integer. How to do it?
     
    Allen, Feb 5, 2007
    #1
    1. Advertising

  2. On Feb 5, 7:15 am, "Allen" <> wrote:
    > 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
     
    Faton Berisha, Feb 5, 2007
    #2
    1. Advertising

  3. Allen

    Guest

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


    On Feb 5, 3:29 pm, "Faton Berisha" <> wrote:
    > On Feb 5, 7:15 am, "Allen" <> wrote:
    >
    > > 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
     
    , Feb 5, 2007
    #3
  4. On 5 Feb 2007 02:32:34 -0800, wrote:
    > 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

    --
    [ 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
     
    Gordon Beaton, Feb 5, 2007
    #4
  5. Allen

    Lew Guest

    Allen wrote:
    > 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
     
    Lew, Feb 5, 2007
    #5
  6. Allen

    Allen Guest

    On 2ÔÂ5ÈÕ, ÏÂÎç2ʱ15·Ö, "Allen" <> wrote:
    > 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.
     
    Allen, Feb 6, 2007
    #6
  7. Allen wrote:
    > On 2ÔÂ5ÈÕ, ÏÂÎç2ʱ15·Ö, "Allen" <> wrote:
    >> 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.
    >


    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
     
    Patricia Shanahan, Feb 6, 2007
    #7
  8. Allen

    Guest

    On Feb 5, 11:15 am, "Allen" <> wrote:
    > 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.
     
    , Feb 6, 2007
    #8
  9. Allen

    John Maline Guest

    Allen wrote:
    > On 2ÔÂ5ÈÕ, ÏÂÎç2ʱ15·Ö, "Allen" <> wrote:
    >> 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
     
    John Maline, Feb 6, 2007
    #9
  10. Allen

    Allen Guest

    On 2ÔÂ7ÈÕ, ÉÏÎç2ʱ44·Ö, John Maline <> wrote:
    > Allen wrote:
    > > On 2ÔÂ5ÈÕ, ÏÂÎç2ʱ15·Ö, "Allen" <> wrote:
    > >> 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


    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, Feb 7, 2007
    #10
  11. Allen

    Guest

    On Feb 6, 7:08 pm, "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.


    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
     
    , Feb 7, 2007
    #11
  12. Allen

    Esmond Pitt Guest

    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.


    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.
     
    Esmond Pitt, Feb 7, 2007
    #12
  13. Allen

    Jeff Higgins Guest

    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]
     
    Jeff Higgins, Feb 7, 2007
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. les ander
    Replies:
    4
    Views:
    902
    wes weston
    Oct 5, 2004
  2. Replies:
    5
    Views:
    1,949
    Wesley Hall
    Nov 4, 2006
  3. Replies:
    3
    Views:
    494
    red floyd
    Feb 20, 2007
  4. Replies:
    1
    Views:
    490
    Mark Rae [MVP]
    Sep 20, 2007
  5. Zeynel
    Replies:
    1
    Views:
    397
    rantingrick
    Nov 10, 2010
Loading...

Share This Page