Bug 14878 - static final char NegThree= (char)-3, -3 == NegThree returns true
Summary: static final char NegThree= (char)-3, -3 == NegThree returns true
Status: RESOLVED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 2.0   Edit
Hardware: PC Windows 2000
: P3 normal (vote)
Target Milestone: 2.0 M6   Edit
Assignee: Philipe Mulet CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2002-04-29 16:47 EDT by Jed Anderson CLA
Modified: 2002-04-30 06:38 EDT (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
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.