Solved Syntax questions.

Discussion in 'Plugin Development' started by Ibas, Aug 9, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    Ibas

    Code:java
    1. if ((players.getWorld() != player.getWorld()) || (players.getLocation().distance(player.getLocation()) > 2000 && !players.hasPermission("priziuretojas.snipas"))) {
    2. event.getRecipients().remove(players);
    3. }


    1) What's the difference between this:
    Code:java
    1. if ((players.getWorld() != player.getWorld())

    And this:
    Code:java
    1. if (players.getWorld() != player.getWorld()


    2) What's the difference between this:
    Code:java
    1. player.getWorld()) || (players.getLocation

    And this:
    Code:java
    1. player.getWorld()) | (players.getLocation
     
  2. Offline

    Tarestudio

    Ibas
    To 1.: Every ( you set needs a ). They are always in pairs and the second misses the ending )
    To 2.: || is the logical OR, while | is a bitwise inclusive OR. So first is for values true and false, second for handling bits and bytes

    EDIT: To 1.: Just saw you missed the ending ) in both cases, so in your example the both are meaning the same, so the ( ) are just working like in math when it comes to * vs +. It just gives the order of evaluate the statements
     
  3. Offline

    Dyprex

    Tarestudio
    Concerning the second question: if you are not dealing with bits and bytes, the | or & tokens are used to make sure that the compiler interprets all parts of the expression.

    For example:
    (false || true)
    The compiler would stop interpreting at false.

    (false | true)
    The compiler would interpret the whole expression.
     
  4. Offline

    Tarestudio

    Dyprex
    1. if(false || true) is true, not false... if (false && true) is false and breaks after false, because it cant get true anyway.
    2. booleans have 1 bit, so true would be 1 and false would be 0. doing a bitwise inclusive OR on that will result 1, would be true. So you dont interpret the whole expression, you just operating on the bits.
     
    Dyprex likes this.
Thread Status:
Not open for further replies.

Share This Page