Farzan Najipour
Farzan Najipour

Reputation: 2493

combining 2 method calls in an if-condition

I have two function which I want to call both of them
first Question: if both of them are true ,some operation will do (in this example add flags). is this way correct to call them?

    if (getCallId()==1 & getCallState()==1 )
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
            | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED

          | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON

            | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
public int getCallState()
{
    return MiniSipService.m_callState;
}
    public int getCallId()
{
    return MiniSipService.m_callId;
}

second Question: What would do I if I wanted to negativate one of function?

Upvotes: 1

Views: 68

Answers (2)

Eran
Eran

Reputation: 393936

It would be more correct to use the short circuit AND operator :

if (getCallId()==1 && getCallState()==1 )

Or even better, change the methods to return boolean:

public boolean getCallState()
{
    return MiniSipService.m_callState == 1;
}

public boolean getCallId()
{
    return MiniSipService.m_callId == 1;
}

and change the condition to:

if (getCallId() && getCallState())

Of course, if m_callId and m_callState can have more than two states, this change will make less sense, but I assumed they have two states based on the wording of your question:

I want to call both of them and if both of them are true...

Upvotes: 1

John3136
John3136

Reputation: 29266

In Java & is a bitwise AND and | is a bitwise OR. For logical operations you should use the logical operators && and ||.

Upvotes: 1

Related Questions