Solved

store a reference to object into string

Posted on 2011-03-07
49
269 Views
Last Modified: 2012-05-11
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
0
Comment
Question by:bachra04
  • 15
  • 10
  • 9
  • +2
49 Comments
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
No - that's not possible. A reference in Java could be stored in a Map, with an appropriate key though
0
 
LVL 26

Expert Comment

by:ksivananth
Comment Utility
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
0
 
LVL 2

Author Comment

by:bachra04
Comment Utility
Ok if I store my object in a map, is it possible later to retrieve it by storing the key into string ?
0
 
LVL 2

Author Comment

by:bachra04
Comment Utility
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)
0
 
LVL 26

Expert Comment

by:ksivananth
Comment Utility
whats  your object type? if its String,

String str = "Your String" ;
0
 
LVL 26

Expert Comment

by:ksivananth
Comment Utility
and then pass str to the API!
0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
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
0
 
LVL 2

Author Comment

by:bachra04
Comment Utility
My object is not of type String
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>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?
0
 
LVL 2

Author Comment

by:bachra04
Comment Utility
yes a java string
0
 
LVL 26

Expert Comment

by:ksivananth
Comment Utility
>>My object is not of type String

either it should have String conversion built in( toString override? ) or you need to build one!
0
 
LVL 2

Author Comment

by:bachra04
Comment Utility
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?
0
 
LVL 26

Expert Comment

by:ksivananth
Comment Utility
>>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!
0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
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.
0
 
LVL 2

Author Comment

by:bachra04
Comment Utility
Thank you yan,
Does the timestamp guarantee me No collision in the hashMap ?
0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
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
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
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.
0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
Well, I think hashcode is better solution, though I never tried it myself - I always had some natural
strings to store as a key
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
you need to generate a hashcode of the object, using something like SHA1 (or MD5)
that will give you a unique value
0
 
LVL 2

Author Comment

by:bachra04
Comment Utility
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
0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
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.
0
 
LVL 26

Expert Comment

by:ksivananth
Comment Utility
>>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!
0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
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
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
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
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 47

Expert Comment

by:for_yan
Comment Utility
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

0
 
LVL 2

Author Comment

by:bachra04
Comment Utility
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)
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
> 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
0
 
LVL 26

Expert Comment

by:ksivananth
Comment Utility
>>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?
0
 
LVL 2

Author Comment

by:bachra04
Comment Utility
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

0
 
LVL 92

Expert Comment

by:objects
Comment Utility
a decent hash (eg.SHA1) or UUID should do the job then
0
 
LVL 26

Expert Comment

by:ksivananth
Comment Utility
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?
0
 
LVL 26

Expert Comment

by:ksivananth
Comment Utility
As I said you should read the API doc well and understand their expectation...
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
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
0
 
LVL 2

Author Comment

by:bachra04
Comment Utility
The API is in Java and accepts Java Strings only.
0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
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.

0
 
LVL 2

Author Comment

by:bachra04
Comment Utility
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).
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>The API is in Java and accepts Java Strings only.

It would help if you could show documentation for the method you're calling.
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>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
0
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 100 total points
Comment Utility
Well, then you can use a hashcode() method to generate string
and store them all in the HashMap with this string as akey
and pass this string  as a key to another part of your program - should not be a problem.
0
 
LVL 2

Author Comment

by:bachra04
Comment Utility
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)

0
 
LVL 92

Accepted Solution

by:
objects earned 300 total points
Comment Utility
> Do SHA1 or UUID guarantee uniqueness ?

UUID does, SHA is pretty close to it

> I was thinking about something simpler :  have a static Counter Object;

thats more complicated :)
for example for a UUID you can just generate one using:

UUID nextId = UUID.randomUUID();
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
Yes you can use a counter. That WOULD guarantee uniqueness
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 100 total points
Comment Utility
A good hashCode implementation for the  object in question should be fine though
0
 
LVL 2

Author Closing Comment

by:bachra04
Comment Utility
Thank you guys, you all contributed very well to solve this issue
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
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
0
 
LVL 26

Expert Comment

by:ksivananth
Comment Utility
>>you all contributed very well to solve this issue

honestly I don't understand how it solves your problem... :(
0
 
LVL 2

Author Comment

by:bachra04
Comment Utility
without unique keys, there is usually possibility of key collision
0
 
LVL 2

Author Comment

by:bachra04
Comment Utility
>> 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 )
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now