Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How to convert an Object reference to an integer?

Reply
Thread Tools

How to convert an Object reference to an integer?

 
 
Allen
Guest
Posts: n/a
 
      02-05-2007
I need to convert an Object reference to an integer. How to do it?

 
Reply With Quote
 
 
 
 
Faton Berisha
Guest
Posts: n/a
 
      02-05-2007
On Feb 5, 7:15 am, "Allen" <che...@naritech.cn> 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

 
Reply With Quote
 
 
 
 
dagarwal82@gmail.com
Guest
Posts: n/a
 
      02-05-2007
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" <fberi...@uni-pr.edu> wrote:
> On Feb 5, 7:15 am, "Allen" <che...@naritech.cn> 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



 
Reply With Quote
 
Gordon Beaton
Guest
Posts: n/a
 
      02-05-2007
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
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      02-05-2007
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
 
Reply With Quote
 
Allen
Guest
Posts: n/a
 
      02-06-2007
On 2月5日, 下午2时15分, "Allen" <che...@naritech.cn> 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.

 
Reply With Quote
 
Patricia Shanahan
Guest
Posts: n/a
 
      02-06-2007
Allen wrote:
> On 2月5日, 下午2时15分, "Allen" <che...@naritech.cn> 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
 
Reply With Quote
 
srinivas.veeranki@gmail.com
Guest
Posts: n/a
 
      02-06-2007
On Feb 5, 11:15 am, "Allen" <che...@naritech.cn> 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.


 
Reply With Quote
 
John Maline
Guest
Posts: n/a
 
      02-06-2007
Allen wrote:
> On 2月5日, 下午2时15分, "Allen" <che...@naritech.cn> 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
 
Reply With Quote
 
Allen
Guest
Posts: n/a
 
      02-07-2007
On 2月7日, 上午2时44分, John Maline <jmal...@ti.com> wrote:
> Allen wrote:
> > On 2月5日, 下午2时15分, "Allen" <che...@naritech.cn> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
"Object reference not set to an instance of an object" Weird thing happens with reference a link nguyentrongkha@gmail.com ASP .Net 1 09-20-2007 09:46 PM
Qestion about convert Object to byte[] and convert it back davidxiongcn@gmail.com Java 5 11-04-2006 04:11 PM
Object creation - Do we really need to create a parent for a derieved object - can't the base object just point to an already created base object jon wayne C++ 9 09-22-2005 02:06 AM
asp.net 2005 question re: "reference to a non-shared member requires an object reference" ce ASP .Net 1 06-23-2005 09:15 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57