Reputation: 25
I have noticed a really weird behaviour in the Code of the HiberSAP Project (BooleanConverter Linenumber 75).
The Problem is that a boolean value is not converted into 'X'. I have debugged the Code and inspected the boolean expression javaValue == Boolean.TRUE
. This expression is interpreted as false because javaValue is Boolean.TRUE
and has the ID 36 and Boolean.TRUE has the ID 33 (The ID is shown in the Variables-View of the Eclipse IDE). I can also inspect all Instances of Boolean and indeed, there are four Instances of java.lang.Boolean!!
Could somebody please explain me why this is happening.
Okay, I think my Question isn't precise enough. The field which should be converted is boolean and not java.lang.Boolean. This must be a Classloader Fault here. In my case the only conversion from boolean to Boolean is done by the JVM. I know every Object comparison should use .equals() (and I fill in a Bug) but in this case it should work as it is. See the following Code:
public static void main(String[] args) {
for(int i = 0; i < 1000; i++){
print(true);
}
}
public static void print(Object value) {
System.out.println(value);
}
This should NOT create 1000 java.lang.Boolean Instances. This should always use java.lang.Boolean.TRUE! And this is the Strange behaviour of the Code that I couldn't understand.
Maybe this are the side effect of the reflection which is done by Hibersap? But I think this should not be a reason for such a behaviour.
Please, I just want to understand.
Upvotes: 0
Views: 175
Reputation: 691765
Each time you're doing new Boolean(true)
or new Boolean(false)
, you create a new instance of Boolean, which is, by definition, different from other instances. Objects, except enums, should never be compared with ==
. Use .equals()
instead. Or, in your case, use javaValue.booleanValue()
.
Upvotes: 3