Bug 14878

Summary: static final char NegThree= (char)-3, -3 == NegThree returns true
Product: [Eclipse Project] JDT Reporter: Jed Anderson <jed.anderson>
Component: CoreAssignee: Philipe Mulet <philippe_mulet>
Status: RESOLVED FIXED QA Contact:
Severity: normal    
Priority: P3 CC: eclipse
Version: 2.0   
Target Milestone: 2.0 M6   
Hardware: PC   
OS: Windows 2000   
Whiteboard:

Description Jed Anderson CLA 2002-04-29 16:47:07 EDT
Build: M5++

Compile the following with javac and run it with java (version 1.4.0-rc-b91)

public class A {
	static final char NegThree= (char)-3;
	
	public static void main(String[] args) {
		System.out.println(-3 == NegThree);
	}
}

You will get false.

Try this with Eclipse, and you will get true. This is wrong.  It appears that 
the compiler is inlining the incorrect value, because if you remove the "final" 
modifier it runs correctly.
Comment 1 Luc Bourlier CLA 2002-04-29 18:33:58 EDT
I think I find the problem.

As both operands are constant, the compiler optimizes the code, it computes the
result of the operation and just put this value in the byte code.

But I think there is a typo in the code which manages that, in
EqualExpression.computeConstant(TypeBinding, TypeBinding). With the following
modification, the behavior of the generated code is correct.

public final void computeConstant(TypeBinding leftTb, TypeBinding rightTb) {
  if ((left.constant != NotAConstant) && (right.constant != NotAConstant)) {
    constant =
      Constant.computeConstantOperationEQUAL_EQUAL(
        left.constant, 
        leftTb.id,          <============= there is the modification
        EQUAL_EQUAL,
        right.constant,
        rightTb.id);
    if (((bits & OperatorMASK) >> OperatorSHIFT) == NOT_EQUAL)
      constant = Constant.fromValue(!constant.booleanValue());
  } else {
    constant = NotAConstant;
  }
}


Comment 2 Philipe Mulet CLA 2002-04-30 06:38:18 EDT
Good find. Thanks.

Fixed.