Link to home
Start Free TrialLog in
Avatar of bachra04
bachra04Flag for Canada

asked on

store a reference to object into string

I have the following problem:
I created an object A and want to store a reference of object A in a string so that I can retrieve it later (similar way as in C++ when we can keep a pointer to an object in uint32). Is there a way to do that in java ?

Thanks
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

No - that's not possible. A reference in Java could be stored in a Map, with an appropriate key though
you don't have to store it in a string, you'll store the reference in its type, for e.g., if A is your class,

A a  = new A() ; //here 'a' holds the reference of instance A and u can use 'a' as the reference later
Avatar of bachra04

ASKER

Ok if I store my object in a map, is it possible later to retrieve it by storing the key into string ?
to Ksivananth :

No I need to store it in reference because I’m using a third party API that allows me to use String only to pass the object handle (reference)
whats  your object type? if its String,

String str = "Your String" ;
and then pass str to the API!
If that third party API is not in Java you probably need to go through JNI (Java Native Interface).
If it is in Java, you usually don't need to worry about the handles, so it is probably after all not in Java
My object is not of type String
>>I’m using a third party API that allows me to use String only to pass the object handle (reference)

What kind of string - a Java string?
yes a java string
>>My object is not of type String

either it should have String conversion built in( toString override? ) or you need to build one!
Yes OK,
Let’s say I override my object toString method().
Then how can I retrieve back my object using only the String  ? is it unique?
>>Then how can I retrieve back my object using only the String  ?

why do you want to retrieve back from String while you have the object reference already!
No you cannot go back from toString() to your object.

What you can do is to generate some unique string - say orresponding to the timestamp
and store your object in the HashMap with that string as a key.
Then if you are dealing with java software you can pass to that software this hashmap and this string as a key
and the information on the object type and then
your object can be retrieved within that software.
Thank you yan,
Does the timestamp guarantee me No collision in the hashMap ?
You can probably also use the hashcode() method of the Object calss to generate
unique string for the object.
However, this string alone will not be enough to retrieve the object back, unless
you store the object itself  in some Map container and give your software access to that container
Java allows no direct pointer access so you can't get a pointer to cast.  It's however conceivable that you could serialize the whole object into a string and then deserialize it, but of course the 'other end' need to know how to do that.
Well, I think hashcode is better solution, though I never tried it myself - I always had some natural
strings to store as a key
you need to generate a hashcode of the object, using something like SHA1 (or MD5)
that will give you a unique value
to CEJH :

Yep I have this possibility.
Does that guarantee uniqueness of the string ?
Is there any sample code how to serialize/deserialize to a string ?

Thanks
Timestamp does not guarantee uniqueness, but when storing the next object you can check the hashmap
and if such key already exists you can pick up next timestamp up to the point when theere is no
such key in the Map - that may slow down a bit your operation if
you plan to get them stored very quickly.
So hashcode should work.
>>Well, I think hashcode is better solution

I don't think it helps... how does the 3rd party app know the valye can be retrieved from a map and how does it get the referenec of the map...

you should read their API doc and see what they expect the String to contain... you can not simply assume and send something on your own!
If you can pass a Map to your third party API, you rather don't want to serialize - that would be
some time consuming ioperation, I guess
No a timestamp is not going to help you. Certainly won't be unique.
If you just want to generate a random id then you can use UUID
thisd is about hashcode() method:

hashCode

public int hashCode()

    Returns a hash code value for the object. This method is supported for the benefit of hashtables such as those provided by java.util.Hashtable.

    The general contract of hashCode is:

        * Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
        * If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
        * It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables.

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

    Returns:
        a hash code value for this object.
    See Also:
        equals(java.lang.Object), Hashtable

So to sum up  : two possible solutions :

1st solution :  using hashMap but ensure the key is unique

2nd solution : serialize /deserialize the into a string ( some extra overhead)
> 2nd solution : serialize /deserialize the into a string ( some extra overhead)

thats not really practical, and offers you no advantages.
and will be very hard to make it unique
>>using hashMap but ensure the key is unique

does the API allows map?

>>serialize /deserialize the into a string ( some extra overhead)

does the API know the data has to be deserialized?
to Ksivananth :

My code  calls the API in the client side and pass the string.

My code on the server side uses the API and get the string

a decent hash (eg.SHA1) or UUID should do the job then
r u  just using the API to pass value from client and get values in Server? doesn't the API do something on the value passed in?
As I said you should read the API doc well and understand their expectation...
As k. said, we need to know something about the api. A hashcode or uuid is irrelevant: what you seem to be wanting is a pointer (which you can't have) but knowing something about how the api works might help.

If it REALLY needs a pointer as a String then you'd probably need to have created a vm in native code. That way, you can manipulate pointers
The API is in Java and accepts Java Strings only.
That sounds kind of confusing.
If you are passing this object from one computer to another
(e.g. from client to server or vice versa) then in general situation you of course cannot rely on any
HashMap conatainers because you'll need to pass your whole HashMap when you
want just one object;  then you should rather pass object itself in serialized form.
It is only in case when both client and server happen to have access to the same HashMap, that
you can just pass only the key between each other.
In this special case the HashMap with hashcodes of the objects as keys should probably work, but you should
be careful that both client and server should have access to the same HashMap.

To  for yan:

They are not running on different machine neither on on different processes, they both have access to the HashMAp (It is kind of Software Design Tool).
>>The API is in Java and accepts Java Strings only.

It would help if you could show documentation for the method you're calling.
>>they both have access to the HashMAp

So there IS a HashMap? If it's shared, then they might want you to pass the key as i mentioned earlier, but all this guesswork is kind of pointless
SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Do SHA1 or UUID guarantee uniqueness ?
I was thinking about something simpler :  have a static Counter Object;
Whenever I add a new object into the Map , I get the Id from the Counter (which will be automatically incremented)

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yes you can use a counter. That WOULD guarantee uniqueness
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you guys, you all contributed very well to solve this issue
You don't need to reinvent Java maps with UUID/SHA btw: all that will happen there if you do is that you'll slow everything down
>>you all contributed very well to solve this issue

honestly I don't understand how it solves your problem... :(
without unique keys, there is usually possibility of key collision
>> honestly I don't understand how it solves your problem... :(

I need to pass and retrieve my object using String  (using HashMap and unique key that has solved my problem )