Hashcode as serialVersionUid
I read in an article that the default serialVersionUid provided by JVM is the hashcode of an object. If we don't override the hashcode method in a class, how will the hashcode be computed during deserializatio as normally hashcode is the memory address of object?
java serialization hashcode serialversionuid
add a comment |
I read in an article that the default serialVersionUid provided by JVM is the hashcode of an object. If we don't override the hashcode method in a class, how will the hashcode be computed during deserializatio as normally hashcode is the memory address of object?
java serialization hashcode serialversionuid
add a comment |
I read in an article that the default serialVersionUid provided by JVM is the hashcode of an object. If we don't override the hashcode method in a class, how will the hashcode be computed during deserializatio as normally hashcode is the memory address of object?
java serialization hashcode serialversionuid
I read in an article that the default serialVersionUid provided by JVM is the hashcode of an object. If we don't override the hashcode method in a class, how will the hashcode be computed during deserializatio as normally hashcode is the memory address of object?
java serialization hashcode serialversionuid
java serialization hashcode serialversionuid
asked Nov 21 '18 at 5:10
TckTck
285
285
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
I read in an article that the default
serialVersionUid
provided by JVM is the hashcode of an object.
That is incorrect. (Either the article is incorrect, or you misread / misunderstood it.)
The default serial version UID for a serializable class is totally unrelated to the hashCode.
The algorithm for generating the defaut serial version UID is described here:
https://docs.oracle.com/javase/7/docs/platform/serialization/spec/class.html#4100.
Basically, it creates an SHA-1 hash from the classes name, modifiers, interface names, and the signatures for its fields, constructors and methods. Then it takes the first 8 bytes of the hash and assembles them into a long
.
Thanks for sharing the link which mentions the approach of generating the SUID. However it also mentions that "If the SUID is not declared for a class, the value defaults to the hash for that class."
– Ketan
Nov 21 '18 at 5:35
That hash is generated using the algorithm that I linked to. Please read it again.
– Stephen C
Nov 21 '18 at 5:36
add a comment |
I think you misunderstood. It is not hashcode
. serialVersionUid
is static variable and hashcode
is instance method and hashcode value of object varies object to object.
serialVersionUid
is calculated based on the structure of your class - fields, methods, etc. It is specified in the http://download.oracle.com/javase/6/docs/platform/serialization/spec/serialTOC.html
http://download.oracle.com/javase/6/docs/platform/serialization/spec/class.html#4100 for the exact format.
The spec describes what happens in no value is provided, but the autogeneration uses the same algorithm.
The sequence of items in the stream is as follows:
The class name.
The class modifiers written as a 32-bit integer.
The name of each interface sorted by name.
For each field of the class sorted by field name (except private
static and private transient fields:
- The name of the field.
- The modifiers of the field written as a 32-bit integer.
- The descriptor of the field.
If a class initializer exists, write out the following:
- The name of the method, .
- The modifier of the method,
java.lang.reflect.Modifier.STATIC, written as a 32-bit integer. - The descriptor of the method, ()V.
For each non-private constructor sorted by method name and signature:
- The name of the method, .
- The modifiers of the method written as a 32-bit integer.
- The descriptor of the method.
For each non-private method sorted by method name and signature:
- The name of the method.
- The modifiers of the method written as a
32-bit integer. - The descriptor of the method.
- The SHA-1 algorithm is executed on the stream of bytes produced by DataOutputStream and produces five 32-bit values sha[0..4].
The hash value is assembled from the first and second 32-bit values of the SHA-1 message digest. If the result of the message digest, the five 32-bit words H0 H1 H2 H3 H4, is in an array of five int values named sha, the hash value would be computed as follows:
long hash
= ((sha[0] >>> 24) & 0xFF) |
((sha[0] >>> 16) & 0xFF) << 8 |
((sha[0] >>> 8) & 0xFF) << 16 |
((sha[0] >>> 0) & 0xFF) << 24 |
((sha[1] >>> 24) & 0xFF) << 32 |
((sha[1] >>> 16) & 0xFF) << 40 |
((sha[1] >>> 8) & 0xFF) << 48 |
((sha[1] >>> 0) & 0xFF) << 56;
Here long hash
is not referred to hashcode
In JVM, there will be ONE instance of each Class. So what the doc says about hashCode is of the Class instance...
– Ketan
Nov 21 '18 at 5:53
Can you please share the doc reference.
– secret super star
Nov 21 '18 at 6:02
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53405593%2fhashcode-as-serialversionuid%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
I read in an article that the default
serialVersionUid
provided by JVM is the hashcode of an object.
That is incorrect. (Either the article is incorrect, or you misread / misunderstood it.)
The default serial version UID for a serializable class is totally unrelated to the hashCode.
The algorithm for generating the defaut serial version UID is described here:
https://docs.oracle.com/javase/7/docs/platform/serialization/spec/class.html#4100.
Basically, it creates an SHA-1 hash from the classes name, modifiers, interface names, and the signatures for its fields, constructors and methods. Then it takes the first 8 bytes of the hash and assembles them into a long
.
Thanks for sharing the link which mentions the approach of generating the SUID. However it also mentions that "If the SUID is not declared for a class, the value defaults to the hash for that class."
– Ketan
Nov 21 '18 at 5:35
That hash is generated using the algorithm that I linked to. Please read it again.
– Stephen C
Nov 21 '18 at 5:36
add a comment |
I read in an article that the default
serialVersionUid
provided by JVM is the hashcode of an object.
That is incorrect. (Either the article is incorrect, or you misread / misunderstood it.)
The default serial version UID for a serializable class is totally unrelated to the hashCode.
The algorithm for generating the defaut serial version UID is described here:
https://docs.oracle.com/javase/7/docs/platform/serialization/spec/class.html#4100.
Basically, it creates an SHA-1 hash from the classes name, modifiers, interface names, and the signatures for its fields, constructors and methods. Then it takes the first 8 bytes of the hash and assembles them into a long
.
Thanks for sharing the link which mentions the approach of generating the SUID. However it also mentions that "If the SUID is not declared for a class, the value defaults to the hash for that class."
– Ketan
Nov 21 '18 at 5:35
That hash is generated using the algorithm that I linked to. Please read it again.
– Stephen C
Nov 21 '18 at 5:36
add a comment |
I read in an article that the default
serialVersionUid
provided by JVM is the hashcode of an object.
That is incorrect. (Either the article is incorrect, or you misread / misunderstood it.)
The default serial version UID for a serializable class is totally unrelated to the hashCode.
The algorithm for generating the defaut serial version UID is described here:
https://docs.oracle.com/javase/7/docs/platform/serialization/spec/class.html#4100.
Basically, it creates an SHA-1 hash from the classes name, modifiers, interface names, and the signatures for its fields, constructors and methods. Then it takes the first 8 bytes of the hash and assembles them into a long
.
I read in an article that the default
serialVersionUid
provided by JVM is the hashcode of an object.
That is incorrect. (Either the article is incorrect, or you misread / misunderstood it.)
The default serial version UID for a serializable class is totally unrelated to the hashCode.
The algorithm for generating the defaut serial version UID is described here:
https://docs.oracle.com/javase/7/docs/platform/serialization/spec/class.html#4100.
Basically, it creates an SHA-1 hash from the classes name, modifiers, interface names, and the signatures for its fields, constructors and methods. Then it takes the first 8 bytes of the hash and assembles them into a long
.
answered Nov 21 '18 at 5:29
Stephen CStephen C
522k70581940
522k70581940
Thanks for sharing the link which mentions the approach of generating the SUID. However it also mentions that "If the SUID is not declared for a class, the value defaults to the hash for that class."
– Ketan
Nov 21 '18 at 5:35
That hash is generated using the algorithm that I linked to. Please read it again.
– Stephen C
Nov 21 '18 at 5:36
add a comment |
Thanks for sharing the link which mentions the approach of generating the SUID. However it also mentions that "If the SUID is not declared for a class, the value defaults to the hash for that class."
– Ketan
Nov 21 '18 at 5:35
That hash is generated using the algorithm that I linked to. Please read it again.
– Stephen C
Nov 21 '18 at 5:36
Thanks for sharing the link which mentions the approach of generating the SUID. However it also mentions that "If the SUID is not declared for a class, the value defaults to the hash for that class."
– Ketan
Nov 21 '18 at 5:35
Thanks for sharing the link which mentions the approach of generating the SUID. However it also mentions that "If the SUID is not declared for a class, the value defaults to the hash for that class."
– Ketan
Nov 21 '18 at 5:35
That hash is generated using the algorithm that I linked to. Please read it again.
– Stephen C
Nov 21 '18 at 5:36
That hash is generated using the algorithm that I linked to. Please read it again.
– Stephen C
Nov 21 '18 at 5:36
add a comment |
I think you misunderstood. It is not hashcode
. serialVersionUid
is static variable and hashcode
is instance method and hashcode value of object varies object to object.
serialVersionUid
is calculated based on the structure of your class - fields, methods, etc. It is specified in the http://download.oracle.com/javase/6/docs/platform/serialization/spec/serialTOC.html
http://download.oracle.com/javase/6/docs/platform/serialization/spec/class.html#4100 for the exact format.
The spec describes what happens in no value is provided, but the autogeneration uses the same algorithm.
The sequence of items in the stream is as follows:
The class name.
The class modifiers written as a 32-bit integer.
The name of each interface sorted by name.
For each field of the class sorted by field name (except private
static and private transient fields:
- The name of the field.
- The modifiers of the field written as a 32-bit integer.
- The descriptor of the field.
If a class initializer exists, write out the following:
- The name of the method, .
- The modifier of the method,
java.lang.reflect.Modifier.STATIC, written as a 32-bit integer. - The descriptor of the method, ()V.
For each non-private constructor sorted by method name and signature:
- The name of the method, .
- The modifiers of the method written as a 32-bit integer.
- The descriptor of the method.
For each non-private method sorted by method name and signature:
- The name of the method.
- The modifiers of the method written as a
32-bit integer. - The descriptor of the method.
- The SHA-1 algorithm is executed on the stream of bytes produced by DataOutputStream and produces five 32-bit values sha[0..4].
The hash value is assembled from the first and second 32-bit values of the SHA-1 message digest. If the result of the message digest, the five 32-bit words H0 H1 H2 H3 H4, is in an array of five int values named sha, the hash value would be computed as follows:
long hash
= ((sha[0] >>> 24) & 0xFF) |
((sha[0] >>> 16) & 0xFF) << 8 |
((sha[0] >>> 8) & 0xFF) << 16 |
((sha[0] >>> 0) & 0xFF) << 24 |
((sha[1] >>> 24) & 0xFF) << 32 |
((sha[1] >>> 16) & 0xFF) << 40 |
((sha[1] >>> 8) & 0xFF) << 48 |
((sha[1] >>> 0) & 0xFF) << 56;
Here long hash
is not referred to hashcode
In JVM, there will be ONE instance of each Class. So what the doc says about hashCode is of the Class instance...
– Ketan
Nov 21 '18 at 5:53
Can you please share the doc reference.
– secret super star
Nov 21 '18 at 6:02
add a comment |
I think you misunderstood. It is not hashcode
. serialVersionUid
is static variable and hashcode
is instance method and hashcode value of object varies object to object.
serialVersionUid
is calculated based on the structure of your class - fields, methods, etc. It is specified in the http://download.oracle.com/javase/6/docs/platform/serialization/spec/serialTOC.html
http://download.oracle.com/javase/6/docs/platform/serialization/spec/class.html#4100 for the exact format.
The spec describes what happens in no value is provided, but the autogeneration uses the same algorithm.
The sequence of items in the stream is as follows:
The class name.
The class modifiers written as a 32-bit integer.
The name of each interface sorted by name.
For each field of the class sorted by field name (except private
static and private transient fields:
- The name of the field.
- The modifiers of the field written as a 32-bit integer.
- The descriptor of the field.
If a class initializer exists, write out the following:
- The name of the method, .
- The modifier of the method,
java.lang.reflect.Modifier.STATIC, written as a 32-bit integer. - The descriptor of the method, ()V.
For each non-private constructor sorted by method name and signature:
- The name of the method, .
- The modifiers of the method written as a 32-bit integer.
- The descriptor of the method.
For each non-private method sorted by method name and signature:
- The name of the method.
- The modifiers of the method written as a
32-bit integer. - The descriptor of the method.
- The SHA-1 algorithm is executed on the stream of bytes produced by DataOutputStream and produces five 32-bit values sha[0..4].
The hash value is assembled from the first and second 32-bit values of the SHA-1 message digest. If the result of the message digest, the five 32-bit words H0 H1 H2 H3 H4, is in an array of five int values named sha, the hash value would be computed as follows:
long hash
= ((sha[0] >>> 24) & 0xFF) |
((sha[0] >>> 16) & 0xFF) << 8 |
((sha[0] >>> 8) & 0xFF) << 16 |
((sha[0] >>> 0) & 0xFF) << 24 |
((sha[1] >>> 24) & 0xFF) << 32 |
((sha[1] >>> 16) & 0xFF) << 40 |
((sha[1] >>> 8) & 0xFF) << 48 |
((sha[1] >>> 0) & 0xFF) << 56;
Here long hash
is not referred to hashcode
In JVM, there will be ONE instance of each Class. So what the doc says about hashCode is of the Class instance...
– Ketan
Nov 21 '18 at 5:53
Can you please share the doc reference.
– secret super star
Nov 21 '18 at 6:02
add a comment |
I think you misunderstood. It is not hashcode
. serialVersionUid
is static variable and hashcode
is instance method and hashcode value of object varies object to object.
serialVersionUid
is calculated based on the structure of your class - fields, methods, etc. It is specified in the http://download.oracle.com/javase/6/docs/platform/serialization/spec/serialTOC.html
http://download.oracle.com/javase/6/docs/platform/serialization/spec/class.html#4100 for the exact format.
The spec describes what happens in no value is provided, but the autogeneration uses the same algorithm.
The sequence of items in the stream is as follows:
The class name.
The class modifiers written as a 32-bit integer.
The name of each interface sorted by name.
For each field of the class sorted by field name (except private
static and private transient fields:
- The name of the field.
- The modifiers of the field written as a 32-bit integer.
- The descriptor of the field.
If a class initializer exists, write out the following:
- The name of the method, .
- The modifier of the method,
java.lang.reflect.Modifier.STATIC, written as a 32-bit integer. - The descriptor of the method, ()V.
For each non-private constructor sorted by method name and signature:
- The name of the method, .
- The modifiers of the method written as a 32-bit integer.
- The descriptor of the method.
For each non-private method sorted by method name and signature:
- The name of the method.
- The modifiers of the method written as a
32-bit integer. - The descriptor of the method.
- The SHA-1 algorithm is executed on the stream of bytes produced by DataOutputStream and produces five 32-bit values sha[0..4].
The hash value is assembled from the first and second 32-bit values of the SHA-1 message digest. If the result of the message digest, the five 32-bit words H0 H1 H2 H3 H4, is in an array of five int values named sha, the hash value would be computed as follows:
long hash
= ((sha[0] >>> 24) & 0xFF) |
((sha[0] >>> 16) & 0xFF) << 8 |
((sha[0] >>> 8) & 0xFF) << 16 |
((sha[0] >>> 0) & 0xFF) << 24 |
((sha[1] >>> 24) & 0xFF) << 32 |
((sha[1] >>> 16) & 0xFF) << 40 |
((sha[1] >>> 8) & 0xFF) << 48 |
((sha[1] >>> 0) & 0xFF) << 56;
Here long hash
is not referred to hashcode
I think you misunderstood. It is not hashcode
. serialVersionUid
is static variable and hashcode
is instance method and hashcode value of object varies object to object.
serialVersionUid
is calculated based on the structure of your class - fields, methods, etc. It is specified in the http://download.oracle.com/javase/6/docs/platform/serialization/spec/serialTOC.html
http://download.oracle.com/javase/6/docs/platform/serialization/spec/class.html#4100 for the exact format.
The spec describes what happens in no value is provided, but the autogeneration uses the same algorithm.
The sequence of items in the stream is as follows:
The class name.
The class modifiers written as a 32-bit integer.
The name of each interface sorted by name.
For each field of the class sorted by field name (except private
static and private transient fields:
- The name of the field.
- The modifiers of the field written as a 32-bit integer.
- The descriptor of the field.
If a class initializer exists, write out the following:
- The name of the method, .
- The modifier of the method,
java.lang.reflect.Modifier.STATIC, written as a 32-bit integer. - The descriptor of the method, ()V.
For each non-private constructor sorted by method name and signature:
- The name of the method, .
- The modifiers of the method written as a 32-bit integer.
- The descriptor of the method.
For each non-private method sorted by method name and signature:
- The name of the method.
- The modifiers of the method written as a
32-bit integer. - The descriptor of the method.
- The SHA-1 algorithm is executed on the stream of bytes produced by DataOutputStream and produces five 32-bit values sha[0..4].
The hash value is assembled from the first and second 32-bit values of the SHA-1 message digest. If the result of the message digest, the five 32-bit words H0 H1 H2 H3 H4, is in an array of five int values named sha, the hash value would be computed as follows:
long hash
= ((sha[0] >>> 24) & 0xFF) |
((sha[0] >>> 16) & 0xFF) << 8 |
((sha[0] >>> 8) & 0xFF) << 16 |
((sha[0] >>> 0) & 0xFF) << 24 |
((sha[1] >>> 24) & 0xFF) << 32 |
((sha[1] >>> 16) & 0xFF) << 40 |
((sha[1] >>> 8) & 0xFF) << 48 |
((sha[1] >>> 0) & 0xFF) << 56;
Here long hash
is not referred to hashcode
answered Nov 21 '18 at 5:39
secret super starsecret super star
1,025115
1,025115
In JVM, there will be ONE instance of each Class. So what the doc says about hashCode is of the Class instance...
– Ketan
Nov 21 '18 at 5:53
Can you please share the doc reference.
– secret super star
Nov 21 '18 at 6:02
add a comment |
In JVM, there will be ONE instance of each Class. So what the doc says about hashCode is of the Class instance...
– Ketan
Nov 21 '18 at 5:53
Can you please share the doc reference.
– secret super star
Nov 21 '18 at 6:02
In JVM, there will be ONE instance of each Class. So what the doc says about hashCode is of the Class instance...
– Ketan
Nov 21 '18 at 5:53
In JVM, there will be ONE instance of each Class. So what the doc says about hashCode is of the Class instance...
– Ketan
Nov 21 '18 at 5:53
Can you please share the doc reference.
– secret super star
Nov 21 '18 at 6:02
Can you please share the doc reference.
– secret super star
Nov 21 '18 at 6:02
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53405593%2fhashcode-as-serialversionuid%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown