0

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?

JJT
  • 186
  • 1
  • 13
  • 1
    Is `filterValue.getIncludeUnrecieved` correct? Shouldn't it be `filterValue.getIncludeUnrecieved()`? – Guilherme Schaidhauer Castro Jan 20 '21 at 19:09
  • Try looking here: https://stackoverflow.com/questions/11072870/does-check-for-full-equality-in-booleans-java – AP11 Jan 20 '21 at 19:10
  • @GuilhermeSchaidhauerCastro typo in my question, not the code. Thanks for pointing it out, I have edited the question. – JJT Jan 20 '21 at 19:15
  • @AP11 - The link you gives describes the difference between `==` and `.equals()`. I address this in the original question already. Nothing in the linked post answers the question I am asking: why `==` is behaving differently in two different environments. – JJT Jan 20 '21 at 19:19
  • Maybe there are differences in the way boxing is handled (wrt ==) in the two environments. – Jeff Holt Jan 20 '21 at 19:20
  • @JeffHolt Would you be able to elaborate or link to something that does? I am not quite sure what would cause that. I appreciate the help. – JJT Jan 20 '21 at 19:23
  • @JJT As you are dealing with the wrapper class `Boolean`, it is possible that somewhere in your code, you are creating 2 different objects. Can you show how you assign the values to those variables? – Jude Niroshan Jan 20 '21 at 19:23
  • 3
    `(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. – PeterMmm Jan 20 '21 at 19:24
  • @JJT I mean, if I run it comparation if `Boolean == Boolean` it returns `false`, even though both `Boolean` objects are set to `true`. So it¨s not that the server is working differently, it's your environment that is not working properly. – AP11 Jan 20 '21 at 19:25
  • @JudeNiroshan They are definitely two different objects. They are set by the user through checkboxes in a form. – JJT Jan 20 '21 at 19:25
  • @AP11 Yes, it is the local environments that are incorrect. The question is still why? – JJT Jan 20 '21 at 19:27
  • @JTT Oh, I thought that you meant the server was wrong. What Java IDE do you use? – AP11 Jan 20 '21 at 19:29
  • `They are set by the user through checkboxes in a form.` I would try to set a debugging breakpoint or output of the real values. If there is no difference btw local and server, you have found an important bug in the JVM. – PeterMmm Jan 20 '21 at 19:31
  • @PeterMmm It is too much to post all the code here. Both are set by checkboxes in a form. When the user enters submit, the values are used. Both environments (local and remote) were tested multiple times using the exact same use process (Launch program, check both boxes, click submit). The results are always the same. the local enters the "if" and the local does not. – JJT Jan 20 '21 at 19:33
  • @PeterMmm Yes, I have already set debugging breakpoints for both environments and see no reason for the `==` should return true. – JJT Jan 20 '21 at 19:35
  • Which are the Java versions of local machine and of server? – Ole V.V. Jan 20 '21 at 19:52
  • 1
    I'll bet the `if` statement 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 but `equal` might not be easy unless you're strictly using the setters. You could execute the `if` inside each setter and break when they're unequal but `equal`. – Jeff Holt Jan 20 '21 at 20:00
  • @OleV.V. both are on 11 currently. I do not have access to the server environment to confirm, but I have asked DevOps and was told it is the same – JJT Jan 20 '21 at 20:00

0 Answers0