[TUTORIAL] Ternary Operators

Discussion in 'Resources' started by sgavster, Dec 25, 2013.

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

    sgavster

    Hi!

    I was playing with the Ternary Operators
    and, I thought it be nice to teach everyone how to use them!

    How could you use these?

    These could be used as a compact if-statement. An example here:
    PHP:
    private ArrayList<StringternaryCooldown = new ArrayList<>();
    public 
    void toggleCooldown(Player p) {
    ternaryCooldown.contains(p.getName()) ? ternaryCooldown.add(p.gerName()) : ternaryCooldown.add(p.getName);
    }
    It will add/remove them from an arraylist.

    Well.. How do I make them?!?

    It's pretty simple. All you have to do is have

    An 'if' statement, without the if.. Here is an example:

    PHP:
    p.sendMessage(p.getGameMode() == GameMode.SURVIVAL);
    then, we want to add a ? and what do to when it returns true

    PHP:
    p.sendMessage(p.getGameMode() == GameMode.SURVIVAL "You are in survival mode!");
    Now, we want to add an 'else' statement.. without the else, which would be the ":" symbol!

    PHP:
    p.sendMessage(p.getGameMode() == GameMode.SURVIVAL "You are in survival mode!" : );
    Now, we need to add what to do if it's in the else statement!

    PHP:
    p.sendMessage(p.getGameMode() == GameMode.SURVIVAL "You are in survival mode!" "You are not in survival mode!");
    And we're done! What this will send the player the message "You are in survival mode!" if they're in survival mode, and if they're not it will say "You are not in survival mode!".

    What would these statements look with if-statements?

    The first example would be like this:

    PHP:
    private ArrayList<StringternaryCooldown = new ArrayList<>();
    public 
    void toggleCooldown(Player p) {
    if(
    ternaryCooldown.contians(p.getName()) {
    ternaryCooldown.remove(p.getName));
    } else {
    ternaryCooldown.add(p.getName());
    }
    }
     
    And 
    the tutorial would be like this:
     
    [
    PHP]if(p.getGameMode() == GameMode.SURVIVAL) {
    p.sendMessage("You are in survival mode!");
    } else {
    p.sendMessage("You are not in survival mode!");
    }
    Other

    if it helped, let me know! if you have a suggestion, let me know!
     
    jusjus112, Skyost and user_90854156 like this.
  2. Offline

    Jake0oo0

    They're called Ternary operators.
     
    sgavster likes this.
  3. Offline

    sgavster

  4. Offline

    bobacadodl

    Nice tutorial, but just to let you know, in your example
    Code:
    public Boolean isAwesome(Player p) {
    return(p.getLevel() > 9000 ? true : false);
    }
    There's no need for a ternary operator :p
    you can just do

    Code:
    public Boolean isAwesome(Player p) {
    return(p.getLevel() > 9000);
    }
     
  5. Offline

    sgavster

    bobacadodl haha, true. I'll find a better example ;p
     
  6. Offline

    macguy8

    sgavster your current example is still flawed - Any time you do Condition ? true : false it would be an ineffective example as you could just return the result of the condition.
     
  7. Offline

    sgavster

    macguy8 I changed it-- not sure if it'll work, currently don't have an IDE open.
     
  8. Offline

    BungeeTheCookie

    Always wondered what those question marks did. Now I know.
     
    Phasesaber likes this.
  9. Offline

    Jake0oo0

    Here's a good one:
    Bukkit.broadcastMessage(ChatColor.DARK_AQUA + "Starting in " + seconds + " second" + (secs == 1 ? "" : "s") + "!");

    Basically this code will decide whether or not to append the plural to second, depending on whether or not it would be proper based on the number of seconds left.
     
  10. Offline

    xTrollxDudex

    sgavster
    You don't need to post java tutorials in Resources. This is basic knowledge everyone should know, I know Java for Dummies covers it after introducing primitive types.
     
  11. Offline

    BungeeTheCookie

    I never knew what a ternary was because no one ever taught me it. I learned Java myself (And from PogoStick29 for some concepts). It is useful learning from people on Bukkit Forums (I do not want to spend money on a Java For Dummies Book when I can just look here).Even though we all have different opinions I think it is useful to post Java tutorials in the Resource section. ;)
     
    sgavster likes this.
  12. Offline

    epicfacecreeper

    I'll play Devil's Advocate (I love ternary operators!). Ternary operators are too compact. It makes almost unreadable code. It encourages attempting to squeeze everything on to one line. They're great for small things, but for anything bigger (You can put ternary operators inside of each other), they give you too much power, making code unreadable. I am extremely guilty of this. I once made a program that tells you if it's a leap year, on one line. I squeezed about 4 inside each other. Ternary operators are my double chocolate cake, the guilty pleasure of coding.
     
    Chinwe, bobacadodl and sgavster like this.
  13. Offline

    xTrollxDudex

  14. Offline

    BungeeTheCookie

    Your the smartest person in the world. Why didn't I think of that? XD :p
     
  15. Offline

    beastman3226

    Ternary Operators make life easier. I use ternaries for checking null to prevent returning null.

    Code:
    return someObject == null ? someDefaultValue : someObject;
    Versus:
    Code:
    if(someObject == null) {
         return someDefaultValue;
    } else {
      return someObject;
    }
     
  16. Offline

    Garris0n

    For the record, I hate these things. While they do make nice one-liners, the code they produce is a pain to read. I am, however, a huge fan of bodyless if/else statements :p
     
  17. Offline

    beastman3226

    Garris0n
    That is harder to read than ternaries!
     
    werter318 likes this.
  18. Offline

    DarkBladee12

    Garris0n Well I'm pretty much a fan of both since I like to produce compact code and I got used to read these^^
     
  19. Offline

    Garris0n


    Code:java
    1. if(player.isFlying())
    2. player.sendMessage("You're flying!");
    3. else
    4. player.sendMessage("You're not flying :(");


    Code:java
    1. player.sendMessage(player.isFlying() ? "You're flying!" : "You're not flying :(");


    I like the if/else better. Now what's awful is this:
    Code:java
    1. if(player.isFlying()) player.sendMessage("You're flying!");

    Yeah, screw those.
     
Thread Status:
Not open for further replies.

Share This Page