chiperortiz
chiperortiz

Reputation: 4991

Core Java What is Wrong with this Simple Syntax

I have a code like this.

public void test()
{
    final boolean passDay = true;
    final int status;
    //if the right side takes place means i need status value below and is set
    //if only the left side is takes place i dont care about status because i will not use it 
    if(!passDay && ((status=loadStatusValue()))== Constants.YORK_CORK_EMPTY_SET)
        System.out.println("inside 1");//I DONT CARE ABOUT STATUS VALUE
    else
    {
        if(passDay)System.out.println("BAMBA");//I DONT CARE ABOUT STATUS VALUE
        else
        {  
            //HERE STATUS ALWAYS WILL HOLD A VALUE SIMPLYBECAUSE RIGHT SIDE
            //IS ALREADY EVALUATED BECAUSE passDay=false and !passDay=true
            System.out.println(status);            
            //I MEAN I USE STATUS ONLY AFTER BEING INITIALIZED  
            //WHY variable status might not have been initialized IS SHOW IF I AM HERE IS BECAUSE STATUS HAS A VALUE.
        }
    }                
}
private boolean compute(){return true;}
private int loadStatusValue(){return Constants.BOTH_YORK_CORK_SET;}    

What i think in this method everything works i use the status int variable when was already set even is not defined in the initialization.

As you can see the passDay is a boolean means only could hold true or false i have try hardcoded with true and false and not compilation error is show when shows a compilation error when instead being hardcode i do something like it.

final boolean passDay = compute();

in fact compute is also returning a boolean could be true or false but in this case a compilation error is show variable status might not have been initialized but can't java realize that even when true status is just not used and when false the status variable is initialized or set it and used later or i am wrong?

Upvotes: 0

Views: 100

Answers (3)

Andy Turner
Andy Turner

Reputation: 140484

&& is a short-circuiting operator. This means that in the following expression:

!passDay && ((status=loadStatusValue()))== Constants.YORK_CORK_EMPTY_SET

The right-hand operand (((status=loadStatusValue()))== Constants.YORK_CORK_EMPTY_SET) is only evaluated if !passDay is true. This means that the assignment of status will only take place if passDay is false.

If you use final boolean passDay = compute();, there is no way of knowing at compile time if compute() will return true or false.

I suspect (without having tried it) that your code will compile with compute() if you use the non-short-circuiting AND operator, &, which will evaluate the right-hand operand even if the left-hand operand is false.


I think that a better approach is just to restructure your code to make this conditional more readable. Conditionals with side effects are error-prone because it is very easy to overlook the side effect when reading the code:

if (passDay) {
  System.out.println("BAMBA");
} else {
  status = loadStatusValue();
  if (status == Constants.YORK_CORK_EMPTY_SET) {
    System.out.println("inside 1");
  } else {
    System.out.println(status);
  }
}

Upvotes: 3

Chris
Chris

Reputation: 46

Within your code, you set passDay to final meaning it's value can never be changed throughout program execution. Bear this in mind whenever you're doing conditional checks, such as if(!passDay ... ) etc.

If you expect, or intend, to have the value passDay changed, then you can initialise to true at the start of execution, but don't delcare as final

Hopefully this clears some things up.

Upvotes: 1

Anirudh
Anirudh

Reputation: 160

Please read about short circuit evaluation in Java - (condition1 && condition2).

The condition preceding the && operator if evaluates to false; the execution does not evaluate the other condition as it is not required. Regardless of the output of condition2 - false && condition2 shall evaluate to false. Try rearranging the order of conditions or initialize the variable first.

hope this helps.

Upvotes: 4

Related Questions