Java Ternary operator associativity demo


/ Published in: Java
Save to your folder(s)



Copy this code and paste it in your HTML
  1. public class TernaryOp {
  2. public static void main(String[] args) {
  3. int i = Integer.parseInt(args[0]);
  4. System.out.printf("%ni=%s",
  5. // switch case with ternary op idiom:
  6. i == 1 ? "one"
  7. : i == 2 ? "two"
  8. : i == 3 ? "three"
  9. : "too many!"
  10. );
  11. }
  12. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.