Am I correct regarding this boolean method? [Solved]

Liam Andersson :

I was looking up how to perfect the movement in Java. The code I found looked like:

public void update() {

    for (int i = 0; i < AMOUNT_KEYS; i++) {
        keysLast[i] = keys[i];  ////Sets buttons pressed this frame equal to the lastKey array
    }

    for (int i = 0; i < AMOUNT_BUTTONS; i++) {
        buttonsLast[i] = buttons[i];    //Sets buttons pressed this frame equal to the lastKey array
    }
}

//keys Methods
public boolean isKeyPressed(int keycode) {
    return keys[keycode];
}

public boolean isKeyUp(int keycode) {
    return !keys[keycode] && keysLast[keycode]; //Does not return 2 values, it returns true if these conditions are met?
}

public boolean isKeyDown(int keycode) {
    return keys[keycode] && !keysLast[keycode];
}

I was wondering if my thought regarding this snippet is correct, my guess is commented next to it:

public boolean isKeyUp(int keycode) {
    return !keys[keycode] && keysLast[keycode];  //Does not return 2 values, it returns true if these conditions are met?
}

public boolean isKeyDown(int keycode) {
    return keys[keycode] && !keysLast[keycode];
}

It seems to my eye that methods isKeyDown and isKeyUp return 2 booleans. I thought that was impossible. But I tested it and it seems like it acts like an if-statement?. So if the statement after the return command is true it will return true?

Am I correct? If so why does it act that way? Is there a doc somewhere I can read about it?

WJS :

First, does your code compile? The following would only be valid if your arrays returned a boolean value.

return !keys[keycode] && keysLast[keycode];

And if keys[keyCode] is true then !keys[KeyCode] would be false and the statement would return false since the rest of the expression need not be evaluated.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=360219&siteId=1