JDK-8163026 : javac generates wrong bytecode for "ternary expression" /w java.lang.Integer
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 7,8,9
  • Priority: P4
  • Status: Closed
  • Resolution: Not an Issue
  • OS: linux_ubuntu
  • CPU: x86_64
  • Submitted: 2016-08-02
  • Updated: 2025-06-20
  • Resolved: 2016-09-19
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 10
10Resolved
Related Reports
Duplicate :  
Description
FULL PRODUCT VERSION :
java version "1.8.0_101"
Java(TM) SE Runtime Environment (build 1.8.0_101-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.101-b13, mixed mode)


ADDITIONAL OS VERSION INFORMATION :
Linux XXX 4.2.0-42-generic #49-Ubuntu SMP Tue Jun 28 21:26:26 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux


A DESCRIPTION OF THE PROBLEM :
Using the "ternary expression" in conjunction with java.lang.Integer can cause invalid bytecode generation

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Using the "ternary expression" as nullcheck, see test-case

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Every test should run without errors
ACTUAL -
testIntegerHashCode and testIntegerBox throwing a NullPointerException

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import org.junit.Test;

public class Ternary {
	@Test
	public void testObjectHashCode() {
		Object o = null;
		Object r = (null == o) ? o : o.hashCode();
	}

	@Test
	public void testIntegerHashCode() {
		Integer o = null;
		Object r = (null == o) ? o : o.hashCode();
	}

	@Test
	public void testIntegerBox() {
		Integer o = null;
		Object r = (null == o) ? o : -o;
	}
}
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Just don't use the "ternary expression"


Comments
There is no bug here, this is the expected behavior. See that Eclipse also behaves the same way. The statement: Object r = (null == o) ? o : -o; of public void testIntegerBox() { Integer o = null; Object r = (null == o) ? o : -o; } gets lowered to Object r = Integer.valueOf((null == o) ? o.intValue() : -o.intValue()) Likewise, the statement Object r = (null == o) ? o : o.hashCode(); in public void testIntegerHashCode() { Integer o = null; Object r = (null == o) ? o : o.hashCode(); } gets lowered to: Object r = Integer.valueOf((null == o) ? o.intValue() : o.hashCode()) In either case the overall ternary expression's type is int and this requires unboxing the null reference Integer
19-09-2016

This is an issue, if object is null, if statement works properly where as ternery operator fails in above case. This is an issue in 7 8 and 9. it is simple case. 7uxx - Fail 8uxx - Fail (Including 8u102) 9 ea b-129 - Fail == -sh-4.1$ /opt/java/jdk1.8.0_102/bin/java Ternary Exception in thread "main" java.lang.NullPointerException at Ternary.testIntegerBox(Ternary.java:15) at Ternary.main(Ternary.java:21) ==
03-08-2016