To my understanding following code should print "true"
, but when I run it it prints "false"
.
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);
}
}
According to JLS §5.1.7. Boxing Conversion:
If the value
p
being boxed istrue
,false
, abyte
, or achar
in the range\u0000
to\u007f
, or anint
orshort
number between-128
and127
(inclusive), then letr1
andr2
be the results of any two boxing conversions ofp
. It is always the case thatr1 == r2
.
However in case of method called via reflection boxed value is always created via new PrimitiveWrapper()
.
Please help me understand this.
Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes. For example, converting an int to an Integer, a double to a Double, and so on. If the conversion goes the other way, this is called unboxing.
Autoboxing and unboxing lets developers write cleaner code, making it easier to read. The technique lets us use primitive types and Wrapper class objects interchangeably and we do not need to perform any typecasting explicitly.
In the java. lang package java provides a separate class for each of the primitive data type namely Byte, Character, Double, Integer, Float, Long, Short. Converting primitive datatype to object is called boxing.
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).
The cited part has been rewritten multiple times, as discussed in Is caching of boxed Byte objects not required by Java 13 SE spec?
You’ve cited the version use up to Java 7:
If the value p being boxed is
true
,false
, abyte
, achar
in the range\u0000
to\u007f
, or anint
orshort
number between-128
and127
, then letr1
andr2
be the results of any two boxing conversions ofp
. It is always the case thatr1 == r2
.
Note that it forgot to mention long
.
In Java 8, the specification says:
If the value
p
being boxed is an integer literal of typeint
between-128
and127
inclusive (§3.10.1), or the boolean literaltrue
orfalse
(§3.10.3), or a character literal between'\u0000'
and'\u007f'
inclusive (§3.10.4), then leta
andb
be the results of any two boxing conversions ofp
. It is always the case thata == b
.
Which applies to literals only.
Since Java 9, the specification says
If the value
p
being boxed is the result of evaluating a constant expression (§15.28) of typeboolean
,char
,short
,int
, orlong
, and the result istrue
,false
, a character in the range'\u0000'
and'\u007f'
inclusive, or an integer in the range-128
to127
inclusive, then leta
andb
be the results of any two boxing conversions ofp
. It is always the case thata == b
.
This now refers to constant expressions, includes long
and forgot about byte
(has been re‑added in version 14). While this is not insisting on a literal value, a reflective method invocation is not a constant expression, so it doesn’t apply.
Even when we use the old specification’s wording, it’s not clear whether the code implementing the reflective method invocation bears a boxing conversion. The original code stems from a time when boxing conversions did not exist, so it performed an explicit instantiation of wrapper objects and as long as the code contains explicit instantiations, there will be no boxing conversion.
In short, the object identity of wrapper instances returned by reflective operations is unspecified.
Looking at it from the implementors point of view, the code handling the first reflective invocation is native code, which is much harder to change than Java code. But since JDK 1.3, these native method accessors get replaced by generated bytecode when the number of invocations crosses a threshold. Since repeated invocations are the performance critical ones, it’s important to look at these generated accessors. Since JDK 9, these generated accessors use the equivalent of boxing conversions.
So running the following adapted test code:
import java.lang.reflect.Method;
public class Test
{
public static boolean testTrue() {
return true;
}
public static void main(String[] args) throws Exception {
int threshold = Boolean.getBoolean("sun.reflect.noInflation")? 0:
Integer.getInteger("sun.reflect.inflationThreshold", 15);
System.out.printf("should use bytecode after %d invocations%n", threshold);
Method m = Test.class.getMethod("testTrue");
for(int i = 0; i < threshold + 10; i++) {
Object trueResult = m.invoke(null);
System.out.printf("%-2d: %b%n", i, trueResult == Boolean.TRUE);
}
}
}
will print under Java 9 and newer:
should use bytecode after 15 invocations
0 : false
1 : false
2 : false
3 : false
4 : false
5 : false
6 : false
7 : false
8 : false
9 : false
10: false
11: false
12: false
13: false
14: false
15: false
16: true
17: true
18: true
19: true
20: true
21: true
22: true
23: true
24: true
Note that you can play around with the JVM options -Dsun.reflect.inflationThreshold=number
, to alter the threshold, and -Dsun.reflect.noInflation=true
, to let Reflection use bytecode immediately.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With