Home > Back-end >  Java && || operators precedent
Java && || operators precedent

Time:11-12

I need help with one if condition in java code

    public class Main
    {
        public static void main(String[] args) {
            String sHeaderStatus = "1";
            Boolean hasButton = false;
            Boolean editableLineStatus =true;
            String sFrom = "REQ";
            int canChangeSupplier = 0;
            if ((sHeaderStatus.equals("1") || canChangeSupplier == 1 && 
            (sHeaderStatus.equals("10") || sHeaderStatus.equals("14") || sHeaderStatus.equals("85") || sHeaderStatus.equals("86") || sHeaderStatus.equals("87"))
            || hasButton && editableLineStatus && !sHeaderStatus.equals("85")) || sFrom.equals("APPROVAL")) {
                String valdiaton ="true11";
            System.out.println(valdiaton);              
            }       
        }

result is true11

    public class Main
    {
        public static void main(String[] args) {
            String sHeaderStatus = "1";
            Boolean hasButton = false;
            Boolean editableLineStatus =false; //changed this one to false
            String sFrom = "REQ";
            int canChangeSupplier = 0;
            if ((sHeaderStatus.equals("1") || canChangeSupplier == 1 && 
            (sHeaderStatus.equals("10") || sHeaderStatus.equals("14") || sHeaderStatus.equals("85") || sHeaderStatus.equals("86") || sHeaderStatus.equals("87"))
            || hasButton && editableLineStatus && !sHeaderStatus.equals("85")) || sFrom.equals("APPROVAL")) {
                String valdiaton ="true11";
            System.out.println(valdiaton);
                
            }
            
        }

result is still true11

I am not able to understood the issue.

Per my understanding...

sHeaderStatus.equals("1") || canChangeSupplier == 1 // gave true
(sHeaderStatus.equals("10") || sHeaderStatus.equals("14") || sHeaderStatus.equals("85") || sHeaderStatus.equals("86") || sHeaderStatus.equals("87"))
            || hasButton   // gave false

become true && false && true && true

Similarly second code would become true && false && false &&true

Am not sure how this become true and below line printed.

CodePudding user response:

&& happens before ||

Write the code like this

if (
  (
    sHeaderStatus.equals("1")
    || canChangeSupplier == 1 && (sHeaderStatus.equals("10") || sHeaderStatus.equals("14") || sHeaderStatus.equals("85") || sHeaderStatus.equals("86") || sHeaderStatus.equals("87"))
    || hasButton && editableLineStatus && !sHeaderStatus.equals("85")
  ) 
  || sFrom.equals("APPROVAL")
)

You can see that you always will have (something) || false with the values given.

And changing only editableLineStatus will modify only the operator "grouping" for hasButton && editableLineStatus && !sHeaderStatus.equals("85").

However, regardless of what you change that to, you have sHeaderStatus.equals("1"), which is true, resulting in logic of

(true || (false && false) || (false && true/false && true)) || false

which, in total, is true, therefore entering the conditional

CodePudding user response:

First, I would recommend the if condition to be staged, this makes the code much cleaner, easier to debug and easier to be understood.

I also would recommend you to redo the complete condition, trying to make it cleaner, and maybe using switches, this will make you understand good habits in coding.

If you get stuck repost a message on this thread trying to explain what you want the code to do, because it's a bit messy.

  • Related