I have the following class:
public class FilterValues { private Boolean includeRecieved; private Boolean includeUnrecieved; //standard Getters and Setters for both Boolean fields... } Elsewhere in the code, in another class, the two fields are being compared to see if both are true or both are false using:
if (filterValue.getIncludeRecieved() == filterValue.getIncludeUnrecieved()) First off, allow me to explain that I realize that the proper method would be to use the .equals() method for this comparison.
My question is that, when given the scenario that both fields are set to true, the above comparison is returning 'true' and entering the if statement when working on my local. However, when posted to our server, the same scenario returns 'false' and will skip the if statement. The issue was fixed by changing the code to:
if (filterValue.getIncludeRecieved().equals(filterValue.getIncludeUnrecieved())) However I cannot find any explanation for why the difference in behavior occurs in the different environments. I have looked into the documentation and could not find anything that would cause this. I need to understand the why so that I can ensure that this bug does not occur elsewhere, since QA is unable to detect it on their local machines either.
Could anyone please explain what is causing the difference in Java's behavior between the two environments?
filterValue.getIncludeUnrecievedcorrect? Shouldn't it befilterValue.getIncludeUnrecieved()?(filterValue.getIncludeRecieved() == filterValue.getIncludeUnrecieved())returns true if both returns the same reference to a Boolean object. As we don't see all code it can be possible that on local you are referencing the same object and on server (by any condition we don't know) not.ifstatement is being executed after different code path executes in the two environments. On one server, the Boolean members are pointing to the same instance of a true or false Boolean. But in the other environment, some code has set the members to different instances of a true or false Boolean. Discovering the call stack where those members become unequal butequalmight not be easy unless you're strictly using the setters. You could execute theifinside each setter and break when they're unequal butequal.