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<String> ternaryCooldown = 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<String> ternaryCooldown = 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!
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 you can just do Code: public Boolean isAwesome(Player p) { return(p.getLevel() > 9000); }
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.
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.
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.
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.
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.
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; }
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
Garris0n Well I'm pretty much a fan of both since I like to produce compact code and I got used to read these^^
Code:java if(player.isFlying()) player.sendMessage("You're flying!");else player.sendMessage("You're not flying :("); Code:java 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 if(player.isFlying()) player.sendMessage("You're flying!"); Yeah, screw those.