Boxing for primitives is not aligned with specifications - why?
To my understanding following code should print "true"
However, when I ran this program it is printing "false"
as output
According to jls-5.1.7
"If the value p being boxed is an integer literal of type int between
-128 and 127 inclusive , or the boolean literal true or false , or a character literal between 'u0000' and
'u007f' inclusive , then let a and b be the results of
any two boxing conversions of p. It is always the case that a == b."
However in case of method called via reflection boxed value is always created via new PrimitiveWrapper()
.
public class Test {
public static boolean testTrue() {
return true;
}
public static void main(String args) throws Exception {
Object trueResult = Test.class.getMethod("testTrue").invoke(null);
System.out.println(trueResult == Boolean.TRUE);
}
}
Please help me understand this.
java autoboxing
|
show 1 more comment
To my understanding following code should print "true"
However, when I ran this program it is printing "false"
as output
According to jls-5.1.7
"If the value p being boxed is an integer literal of type int between
-128 and 127 inclusive , or the boolean literal true or false , or a character literal between 'u0000' and
'u007f' inclusive , then let a and b be the results of
any two boxing conversions of p. It is always the case that a == b."
However in case of method called via reflection boxed value is always created via new PrimitiveWrapper()
.
public class Test {
public static boolean testTrue() {
return true;
}
public static void main(String args) throws Exception {
Object trueResult = Test.class.getMethod("testTrue").invoke(null);
System.out.println(trueResult == Boolean.TRUE);
}
}
Please help me understand this.
java autoboxing
1
Strictly speaking,Boolean.TRUE
is not the "result of a boxing conversion".
– Jim Garrison
3 hours ago
Please can you fix the odd characters in the JLS quote.
– Andy Turner
3 hours ago
Ok, there is no auto-boxing. This part of the JLS is about auto-boxing
– kumesana
3 hours ago
@BackSlash - Andy turner is abs right, we can not assign that
– Show Stopper
3 hours ago
Well, "in case of a reflection" is not covered by that part of the JLS you're quoting. That part is a continuity about variable conversion when you have a value of a type that you assign to another type using the language normally. Reflection is not a part of that.
– kumesana
3 hours ago
|
show 1 more comment
To my understanding following code should print "true"
However, when I ran this program it is printing "false"
as output
According to jls-5.1.7
"If the value p being boxed is an integer literal of type int between
-128 and 127 inclusive , or the boolean literal true or false , or a character literal between 'u0000' and
'u007f' inclusive , then let a and b be the results of
any two boxing conversions of p. It is always the case that a == b."
However in case of method called via reflection boxed value is always created via new PrimitiveWrapper()
.
public class Test {
public static boolean testTrue() {
return true;
}
public static void main(String args) throws Exception {
Object trueResult = Test.class.getMethod("testTrue").invoke(null);
System.out.println(trueResult == Boolean.TRUE);
}
}
Please help me understand this.
java autoboxing
To my understanding following code should print "true"
However, when I ran this program it is printing "false"
as output
According to jls-5.1.7
"If the value p being boxed is an integer literal of type int between
-128 and 127 inclusive , or the boolean literal true or false , or a character literal between 'u0000' and
'u007f' inclusive , then let a and b be the results of
any two boxing conversions of p. It is always the case that a == b."
However in case of method called via reflection boxed value is always created via new PrimitiveWrapper()
.
public class Test {
public static boolean testTrue() {
return true;
}
public static void main(String args) throws Exception {
Object trueResult = Test.class.getMethod("testTrue").invoke(null);
System.out.println(trueResult == Boolean.TRUE);
}
}
Please help me understand this.
java autoboxing
java autoboxing
edited 3 hours ago
Show Stopper
asked 3 hours ago
Show StopperShow Stopper
5,0381963
5,0381963
1
Strictly speaking,Boolean.TRUE
is not the "result of a boxing conversion".
– Jim Garrison
3 hours ago
Please can you fix the odd characters in the JLS quote.
– Andy Turner
3 hours ago
Ok, there is no auto-boxing. This part of the JLS is about auto-boxing
– kumesana
3 hours ago
@BackSlash - Andy turner is abs right, we can not assign that
– Show Stopper
3 hours ago
Well, "in case of a reflection" is not covered by that part of the JLS you're quoting. That part is a continuity about variable conversion when you have a value of a type that you assign to another type using the language normally. Reflection is not a part of that.
– kumesana
3 hours ago
|
show 1 more comment
1
Strictly speaking,Boolean.TRUE
is not the "result of a boxing conversion".
– Jim Garrison
3 hours ago
Please can you fix the odd characters in the JLS quote.
– Andy Turner
3 hours ago
Ok, there is no auto-boxing. This part of the JLS is about auto-boxing
– kumesana
3 hours ago
@BackSlash - Andy turner is abs right, we can not assign that
– Show Stopper
3 hours ago
Well, "in case of a reflection" is not covered by that part of the JLS you're quoting. That part is a continuity about variable conversion when you have a value of a type that you assign to another type using the language normally. Reflection is not a part of that.
– kumesana
3 hours ago
1
1
Strictly speaking,
Boolean.TRUE
is not the "result of a boxing conversion".– Jim Garrison
3 hours ago
Strictly speaking,
Boolean.TRUE
is not the "result of a boxing conversion".– Jim Garrison
3 hours ago
Please can you fix the odd characters in the JLS quote.
– Andy Turner
3 hours ago
Please can you fix the odd characters in the JLS quote.
– Andy Turner
3 hours ago
Ok, there is no auto-boxing. This part of the JLS is about auto-boxing
– kumesana
3 hours ago
Ok, there is no auto-boxing. This part of the JLS is about auto-boxing
– kumesana
3 hours ago
@BackSlash - Andy turner is abs right, we can not assign that
– Show Stopper
3 hours ago
@BackSlash - Andy turner is abs right, we can not assign that
– Show Stopper
3 hours ago
Well, "in case of a reflection" is not covered by that part of the JLS you're quoting. That part is a continuity about variable conversion when you have a value of a type that you assign to another type using the language normally. Reflection is not a part of that.
– kumesana
3 hours ago
Well, "in case of a reflection" is not covered by that part of the JLS you're quoting. That part is a continuity about variable conversion when you have a value of a type that you assign to another type using the language normally. Reflection is not a part of that.
– kumesana
3 hours ago
|
show 1 more comment
2 Answers
2
active
oldest
votes
invoke will always return a new Object
. Any returned primitives are boxed.
...if the [return] value has a primitive type, it is first appropriately wrapped in an object.
Your issue is demonstrating the ambiguity of the term appropriately. i.e. during wrapping, it does not use Boolean.valueOf(boolean).
1
Just to add a suggestion of the reason why it might be so: the reflection API was added in 1.1;Boolean.valueOf
was added in 1.4. Perhaps the pre-valueOf behavior was retained for backwards compatibility.
– Andy Turner
3 hours ago
add a comment |
1.
The specific
in case of method called via reflection
is not covered by that part of the JLS you're quoting. That part you're quoting is about type conversion when you have a value of a type that you pass as another type. Here you're thinking of converting boolean to Boolean.
But type conversion means doing something like that:
Boolean b = true;
or
boolean b = true;
Boolean b2 = b;
Reflection is not a mechanism that applies type conversion.
When, by necessity, a reflective method call wraps a boolean return value into a Boolean object, it is not involved in the part of the JLS you quoted.
This explains why the JLS is not being violated here.
2.
As to why the reflection isn't choosing to be consistent with this behavior anyway:
That is because in older versions of Java, reflection existed before generics. And generics are the reason why autoboxing suddenly became convenient, and autoboxing is the reason why it seemed smart to not duplicate the "common" values of wrapped primitives.
All of this was defined after reflection already existed for a while, and was already behaving in a specific way. That means that there was already existing Java code that was using reflection, and most likely some existing code that was incorrectly relying on the existing behavior. Changing the existing behavior would have broken existing code, which was therefore avoided.
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%2f54087689%2fboxing-for-primitives-is-not-aligned-with-specifications-why%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
invoke will always return a new Object
. Any returned primitives are boxed.
...if the [return] value has a primitive type, it is first appropriately wrapped in an object.
Your issue is demonstrating the ambiguity of the term appropriately. i.e. during wrapping, it does not use Boolean.valueOf(boolean).
1
Just to add a suggestion of the reason why it might be so: the reflection API was added in 1.1;Boolean.valueOf
was added in 1.4. Perhaps the pre-valueOf behavior was retained for backwards compatibility.
– Andy Turner
3 hours ago
add a comment |
invoke will always return a new Object
. Any returned primitives are boxed.
...if the [return] value has a primitive type, it is first appropriately wrapped in an object.
Your issue is demonstrating the ambiguity of the term appropriately. i.e. during wrapping, it does not use Boolean.valueOf(boolean).
1
Just to add a suggestion of the reason why it might be so: the reflection API was added in 1.1;Boolean.valueOf
was added in 1.4. Perhaps the pre-valueOf behavior was retained for backwards compatibility.
– Andy Turner
3 hours ago
add a comment |
invoke will always return a new Object
. Any returned primitives are boxed.
...if the [return] value has a primitive type, it is first appropriately wrapped in an object.
Your issue is demonstrating the ambiguity of the term appropriately. i.e. during wrapping, it does not use Boolean.valueOf(boolean).
invoke will always return a new Object
. Any returned primitives are boxed.
...if the [return] value has a primitive type, it is first appropriately wrapped in an object.
Your issue is demonstrating the ambiguity of the term appropriately. i.e. during wrapping, it does not use Boolean.valueOf(boolean).
edited 3 hours ago
answered 3 hours ago
OldCurmudgeonOldCurmudgeon
51.5k1384167
51.5k1384167
1
Just to add a suggestion of the reason why it might be so: the reflection API was added in 1.1;Boolean.valueOf
was added in 1.4. Perhaps the pre-valueOf behavior was retained for backwards compatibility.
– Andy Turner
3 hours ago
add a comment |
1
Just to add a suggestion of the reason why it might be so: the reflection API was added in 1.1;Boolean.valueOf
was added in 1.4. Perhaps the pre-valueOf behavior was retained for backwards compatibility.
– Andy Turner
3 hours ago
1
1
Just to add a suggestion of the reason why it might be so: the reflection API was added in 1.1;
Boolean.valueOf
was added in 1.4. Perhaps the pre-valueOf behavior was retained for backwards compatibility.– Andy Turner
3 hours ago
Just to add a suggestion of the reason why it might be so: the reflection API was added in 1.1;
Boolean.valueOf
was added in 1.4. Perhaps the pre-valueOf behavior was retained for backwards compatibility.– Andy Turner
3 hours ago
add a comment |
1.
The specific
in case of method called via reflection
is not covered by that part of the JLS you're quoting. That part you're quoting is about type conversion when you have a value of a type that you pass as another type. Here you're thinking of converting boolean to Boolean.
But type conversion means doing something like that:
Boolean b = true;
or
boolean b = true;
Boolean b2 = b;
Reflection is not a mechanism that applies type conversion.
When, by necessity, a reflective method call wraps a boolean return value into a Boolean object, it is not involved in the part of the JLS you quoted.
This explains why the JLS is not being violated here.
2.
As to why the reflection isn't choosing to be consistent with this behavior anyway:
That is because in older versions of Java, reflection existed before generics. And generics are the reason why autoboxing suddenly became convenient, and autoboxing is the reason why it seemed smart to not duplicate the "common" values of wrapped primitives.
All of this was defined after reflection already existed for a while, and was already behaving in a specific way. That means that there was already existing Java code that was using reflection, and most likely some existing code that was incorrectly relying on the existing behavior. Changing the existing behavior would have broken existing code, which was therefore avoided.
add a comment |
1.
The specific
in case of method called via reflection
is not covered by that part of the JLS you're quoting. That part you're quoting is about type conversion when you have a value of a type that you pass as another type. Here you're thinking of converting boolean to Boolean.
But type conversion means doing something like that:
Boolean b = true;
or
boolean b = true;
Boolean b2 = b;
Reflection is not a mechanism that applies type conversion.
When, by necessity, a reflective method call wraps a boolean return value into a Boolean object, it is not involved in the part of the JLS you quoted.
This explains why the JLS is not being violated here.
2.
As to why the reflection isn't choosing to be consistent with this behavior anyway:
That is because in older versions of Java, reflection existed before generics. And generics are the reason why autoboxing suddenly became convenient, and autoboxing is the reason why it seemed smart to not duplicate the "common" values of wrapped primitives.
All of this was defined after reflection already existed for a while, and was already behaving in a specific way. That means that there was already existing Java code that was using reflection, and most likely some existing code that was incorrectly relying on the existing behavior. Changing the existing behavior would have broken existing code, which was therefore avoided.
add a comment |
1.
The specific
in case of method called via reflection
is not covered by that part of the JLS you're quoting. That part you're quoting is about type conversion when you have a value of a type that you pass as another type. Here you're thinking of converting boolean to Boolean.
But type conversion means doing something like that:
Boolean b = true;
or
boolean b = true;
Boolean b2 = b;
Reflection is not a mechanism that applies type conversion.
When, by necessity, a reflective method call wraps a boolean return value into a Boolean object, it is not involved in the part of the JLS you quoted.
This explains why the JLS is not being violated here.
2.
As to why the reflection isn't choosing to be consistent with this behavior anyway:
That is because in older versions of Java, reflection existed before generics. And generics are the reason why autoboxing suddenly became convenient, and autoboxing is the reason why it seemed smart to not duplicate the "common" values of wrapped primitives.
All of this was defined after reflection already existed for a while, and was already behaving in a specific way. That means that there was already existing Java code that was using reflection, and most likely some existing code that was incorrectly relying on the existing behavior. Changing the existing behavior would have broken existing code, which was therefore avoided.
1.
The specific
in case of method called via reflection
is not covered by that part of the JLS you're quoting. That part you're quoting is about type conversion when you have a value of a type that you pass as another type. Here you're thinking of converting boolean to Boolean.
But type conversion means doing something like that:
Boolean b = true;
or
boolean b = true;
Boolean b2 = b;
Reflection is not a mechanism that applies type conversion.
When, by necessity, a reflective method call wraps a boolean return value into a Boolean object, it is not involved in the part of the JLS you quoted.
This explains why the JLS is not being violated here.
2.
As to why the reflection isn't choosing to be consistent with this behavior anyway:
That is because in older versions of Java, reflection existed before generics. And generics are the reason why autoboxing suddenly became convenient, and autoboxing is the reason why it seemed smart to not duplicate the "common" values of wrapped primitives.
All of this was defined after reflection already existed for a while, and was already behaving in a specific way. That means that there was already existing Java code that was using reflection, and most likely some existing code that was incorrectly relying on the existing behavior. Changing the existing behavior would have broken existing code, which was therefore avoided.
answered 3 hours ago
kumesanakumesana
1,774137
1,774137
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f54087689%2fboxing-for-primitives-is-not-aligned-with-specifications-why%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
1
Strictly speaking,
Boolean.TRUE
is not the "result of a boxing conversion".– Jim Garrison
3 hours ago
Please can you fix the odd characters in the JLS quote.
– Andy Turner
3 hours ago
Ok, there is no auto-boxing. This part of the JLS is about auto-boxing
– kumesana
3 hours ago
@BackSlash - Andy turner is abs right, we can not assign that
– Show Stopper
3 hours ago
Well, "in case of a reflection" is not covered by that part of the JLS you're quoting. That part is a continuity about variable conversion when you have a value of a type that you assign to another type using the language normally. Reflection is not a part of that.
– kumesana
3 hours ago