JDK-7058838 : Changed behavior of compound assignment (+=)
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 7
  • Priority: P4
  • Status: Closed
  • Resolution: Not an Issue
  • OS: windows_7
  • CPU: x86
  • Submitted: 2011-06-24
  • Updated: 2012-03-20
  • Resolved: 2011-06-30
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 6
6-poolResolved
Related Reports
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.7.0-ea"
Java(TM) SE Runtime Environment (build 1.7.0-ea-b144)
Java HotSpot(TM) Client VM (build 21.0-b14, mixed mode, sharing)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows [Version 6.1.7601]

A DESCRIPTION OF THE PROBLEM :
A following function cannot be compiled in java 1.6 or less,. but it can be compiled in java 1.7.

public static void main(String[] args) {
       Object x = "x";
       String y = "y";
       x += i;
}

I read http://jdk7.java.net/preview/ and I didn't find this changed behavior in new features.

REGRESSION.  Last worked in version 6u26

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Just write this function
public static void main(String[] args) {
       Object x = "x";
       String y = "y";
       x += i;
}
in any class and try to compile it in different version of jdk.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
I was expecting that it will not be compiled.

Test.java:6: incompatible types
found   : java.lang.Object
required: java.lang.String
                x += i;
                ^
1 error
ACTUAL -
It was compiled.

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------

public class Test {
	public static void main(String[] args) {
		Object x = "A";
		String i = "B";
		x += i;
	}
}

---------- END SOURCE ----------

Comments
EVALUATION For an Object x and a String y, x+=y is just x=(Object)(x+y). Since y is a String, x undergoes string conversion to produce a string which is concatenated with y, before the no-op cast to Object. The JLS has not changed in this area between SE 6 and SE 7; the program should have been legal for many years.
29-06-2011