Home arrow static arrow Java Programming [Archive] - Meeting trouble when retrieving a byte array.
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Meeting trouble when retrieving a byte array.
This topic has 47 replies on 4 pages.    « Previous | 1 | 2 | 3 | 4 | Next »

Posts:2,909
Registered: 13.8.2003
Re: Meeting trouble when retrieving a byte array.  
Aug 4, 2004 2:18 AM (reply 30 of 47)



 
Ah, you're correct, he did say that it doesn't reflect the address in any way.

Disregard!

And jsalonen, please be more careful next time ;)
 

Posts:536
Registered: 6/29/03
Re: Meeting trouble when retrieving a byte array.  
Aug 4, 2004 10:27 PM (reply 31 of 47)



 
Thanks, jsalonen buddy!

George

 

Posts:536
Registered: 6/29/03
Re: Meeting trouble when retrieving a byte array.  
Aug 4, 2004 10:35 PM (reply 32 of 47)



 
You are always correct too, Kayaman buddy! And I am always not correct. :)

George

 

Posts:536
Registered: 6/29/03
Re: Meeting trouble when retrieving a byte array.  
Aug 4, 2004 10:48 PM (reply 33 of 47)



 
Thanks, jsalonen buddy!

Two more questions,

- What can I do with this hash code when programming? I am a Java developer.

- You mentioned "hashcode of an object cannot change when the garbage collector moves objects around in memory", and also "Even if the value was originally obtained by converting the internal address to an integer it would not be the address of the object after a while". So, in the first case, the hash value will not change and in the second one, the value will change. And it makes me puzzled when it will change and when vice versa. Can you give me a more detailed description?

Best regards,
George

 

Posts:536
Registered: 6/29/03
Re: Meeting trouble when retrieving a byte array.  
Aug 4, 2004 10:51 PM (reply 34 of 47)



 
Thanks, silk buddy!

It is also my original idea. But I have not verified it from formal Java specification. Have you verified your idea through any formal materials?

Best regards,
George

 

Posts:536
Registered: 6/29/03
Re: Meeting trouble when retrieving a byte array.  
Aug 4, 2004 10:54 PM (reply 35 of 47)



 
Thanks, Kayaman buddy!

In any books or specifications mentioned it is an address? I also agree with your idea but can not find any formal verfications. Can you help?

Best regards,
George

 

Posts:2,909
Registered: 13.8.2003
Re: Meeting trouble when retrieving a byte array.  
Aug 4, 2004 11:07 PM (reply 36 of 47)



 
In any books or specifications mentioned it is an
address? I also agree with your idea but can not find
any formal verfications. Can you help?

Well, like silk.m or jsalonen or who was it said, it's in the API. In the hashCode() method of Object (the Object class, not every object) is said:
"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.
However as jsalonen pointed out, the virtual machine can move objects around in the memory so the value returned by hashcode may not be the actual current memory address of the object.
 

Posts:2,830
Registered: 9/1/03
Re: Meeting trouble when retrieving a byte array.  
Aug 5, 2004 1:32 AM (reply 37 of 47)



 
It is also my original idea. But I have not verified
it from formal Java specification. Have you verified
your idea through any formal materials?

yes.

java provides no mechanism to get an object based on a memory address.

so it is impossible.

i wonder if you have started re-writing it yet ;) :)
 

Posts:5,904
Registered: 04/03/99
Re: Meeting trouble when retrieving a byte array.  
Aug 5, 2004 2:05 AM (reply 38 of 47)



 
You mentioned "hashcode of an object cannot change when the garbage collector moves objects around in memory", and also "Even if the value was originally obtained by converting the internal address to an integer it would not be the address of the object after a while".

So, in the first case, the hash value will not change and in the second one, the value will change. And it makes me puzzled when it will change and when vice versa. Can you give me a more detailed description?


You're puzzled because you're getting the hashcode and the internal address mixed up. They are different things - which is why you can't use one (the hashcode) to obtain the other (the internal address), and therefore why you can't use the hashcode to obtain the object.

The hashCode for an object, by default, is created from the address of the object. Suppose the address of the original object is 00000042, that might be the hashcode value. At some later time, the object may get moved to address 00000133. The hashcode is not allowed to change, so although the address will change, the hashcode will not.

Given only the hashcode, or the textual representation of it that you get (by default) from toString, you absolutely, categorically, cannot get the original object. Except possibly by luck, or divine intervention.

Do not even think about calling me "buddy".

Dave.
 

Posts:11,200
Registered: 7/22/99
Re: Meeting trouble when retrieving a byte array.  
Aug 5, 2004 2:06 AM (reply 39 of 47)



 
- What can I do with this hash code when programming?
I am a Java developer.
Hash codes have many uses, one of the most important is that you can make hash tables with them.
http://en.wikipedia.org/wiki/Hash_value
http://en.wikipedia.org/wiki/Hash_table

- You mentioned "hashcode of an object cannot change
when the garbage collector moves objects around in
memory", and also "Even if the value was originally
obtained by converting the internal address to an
integer it would not be the address of the object
after a while". So, in the first case, the hash value
will not change and in the second one, the value will
change. And it makes me puzzled when it will change
and when vice versa. Can you give me a more detailed
description?
The hash value of an object should not change, but the address of an object may well change because the GC is free to move an object from one memory location to another whenever it wants.
 

Posts:5,904
Registered: 04/03/99
Re: Meeting trouble when retrieving a byte array.  
Aug 5, 2004 2:10 AM (reply 40 of 47)



 

By the way, in a (much) earlier post, you said:

"someone input parameter "buffer" to me, whose type is byte[]"

Who is 'someone' and why can't you ask them to give you an object reference instead ?

ie instead of making your API this
public void foo(char[] reference) {   // Futile attempts to determine the object from   // an array of characters}

Make it this...
public void foo(Object reference) {   // Much more promising...}

What's the purpose of the exercise ?

Dave.
 

Posts:536
Registered: 6/29/03
Re: Meeting trouble when retrieving a byte array.  
Aug 5, 2004 7:58 PM (reply 41 of 47)



 
Thanks, dcminter buddy!

I am going to ask him to change the interface. :)

Have a nice weekend,
George

 

Posts:536
Registered: 6/29/03
Re: Meeting trouble when retrieving a byte array.  
Aug 5, 2004 8:08 PM (reply 42 of 47)



 
Thanks, jsalonen buddy!

Very useful resources.

Best regards,
George

 

Posts:536
Registered: 6/29/03
Re: Meeting trouble when retrieving a byte array.  
Aug 5, 2004 8:13 PM (reply 43 of 47)



 
Thanks again, Dave,

Whether the values such as "[B@2aa02aa0" are hash code value of internal address value?

Have a nice weekend,
George

 

Posts:536
Registered: 6/29/03
Re: Meeting trouble when retrieving a byte array.  
Aug 5, 2004 8:15 PM (reply 44 of 47)



 
Not yet silk buddy,

If you have finished a customized version of VM, I will use yours. I trust you. :)

Have a nice weekend,
George

 
This topic has 47 replies on 4 pages.    « Previous | 1 | 2 | 3 | 4 | Next »