Ternary in Java, when to use it, and why

Discussion in 'Resources' started by Barinade, Mar 25, 2014.

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

    Barinade

    Ternary in Java is basically a shortened if-then-else statement.
    You should use it when you are setting fields based on a condition or when your if-then-else statement only has one line per codeblock
    It's mostly for cleaner code

    Here's an example

    Code:
    boolean test = true;
    System.out.println(test?"test is true":"test is false");
    You have your condition, followed by the question mark ternary operator (if), followed by your result if the condition is met, then the colon operator (else), and your result if the condition is not met.

    A more Bukkit API related example
    Code:
    p.getInventory().setHelmet(p.hasPermission("goldhelmet") ? new ItemStack(Material.GOLD_HELMET) : new ItemStack(Material.LEATHER_HELMET));
    This line will set the player's helmet to gold if he has the permission "goldhelmet", otherwise it sets it to a leather helmet, all on one line, rather than:
    Code:
    if (p.hasPermission("goldhelmet")) {
      p.getInventory().setHelmet(new ItemStack(Material.GOLD_HELMET));
    } else {
      p.getInventory().setHelmet(new ItemStack(Material.LEATHER_HELMET));
    }
    So, a follow up, the anatomy of ternary
    condition ? ifMet : ifNotMet

    Everything on this post was written outside of IDE, possible syntax errors if you copy pasta.

    If you're having trouble at this point feel free to contact me for direct help.
     
  2. Offline

    Garris0n

    A note: Please, PLEASE don't overuse these to the point that it starts to ruin readability. As fun as it is to try to pack an entire method into one line, it's awful to try to see what's going on later.
     
  3. Offline

    L33m4n123

    So. What I am now interrested in. Are there any advantages using Ternary over "normal" If - then - else statements during runtime / compiling as I personally tend to use the "normal" If then else statements as its just, at least for me, easier to read
     
  4. Offline

    Barinade

    Nope, no difference. It's just to help sum your code up and make it less messy.

    Garris0n is talking about this (i think)
    condition ? true : condition2 ? true : condition3 ? true : false
    That there is just horrible way to use ternary, ternary is for an if-then-else statement where each codeblock only has one line (or setting variables based on a condition)
     
    Garris0n likes this.
  5. Offline

    RawCode

    you can linebreak your ternary, this allows to build well structured code without "IFELSE overhead"
     
    Garris0n and skyrimfan1 like this.
  6. Offline

    skyrimfan1

    As for what not to do, I made a method that basically returned this:
    Code:java
    1. public double someRandomMethod(double ...set) {
    2. return set.length % 2 == 0 ? (set.length % 4 == 0 ? subMethodOne(set) : (set.length % 8 == 0 ? subMethodOneOne(set) : subMethodOneTwo(set))) : (set.length % 4 == 1 ? subMethodThree(set) : subMethodFour(set));
    3. }

    And inside the sub methods were more ternary operations. :p
    Got kinda wacky but it made the method extremely compact ...
     
  7. Offline

    AoH_Ruthless

    skyrimfan1
    Readability > Compactness, although compactness is definitely important.
     
  8. Offline

    skyrimfan1

     
  9. Offline

    AoH_Ruthless

    skyrimfan1
    I was agreeing with you .., sorry for being vague.
     
    skyrimfan1 likes this.
Thread Status:
Not open for further replies.

Share This Page