Hello guys, if you are wondering how to use ternary operator in Java then you have come to the right place. Ternary operator is a great operator and you can use ternary operator to replace a simple if-else statement. Since ternary operator allows you to write code in one line, its much more succinct and readable and that's what many Java developer love it, including me. In the last article, I shared 10 example of XOR bitwise operator in Java and in this article, I am going to share 10 example of ternary operator in Java so that you not only know what is ternary operator but also you can use it effectively to write better Java code. But, before we get to the 10 best examples that will teach you everything there is to know about ternary operators in Java, let me tell you a little bit more about what it all really is.
The first thing you need to understand is that operators are actually the most basic building blocks of a programming language. In a programming language, operators allow you to perform many calculations and functions like arithmetic, logic, and rationale. Operators are classified according to their functionality.
The ternary operator in Java is a conditional operator that takes three operands. It can act as a replacement for the if-then-else statement and can also be used a lot in Java programming. You can use the ternary operator in place of if-else conditions or you can also switch conditions with the help of nested ternary operators.
10 Examples Of Ternary Operator In Java
Here we have compiled a list of 10 examples of the ternary operator in Java. Keep reading to find out more.
1. Ternary Operator as an alternative to If-Else
public class TernaryOperatorDemo2{ public static void main(String[] args) { int x = 5; int y = 10; String resultValue=(x>=y)?"x is greater than or maybe equal to y":"x is less than y"; System.out.println(resultValue); //o/p is x is less than y } }
2. Using The Ternary Operator as an alternative to if-else-if
public class TernaryOperatorDemo3{ public static void main(String[] args) { int percentage=70; if(percentage>=60){ System.out.println("A grade"); }else if(percentage>=40){ System.out.println("B grade"); }else { System.out.println("Not Eligible"); } } }
public class TernaryOperatorDemo4{ public static void main(String[] args) { int percentage=70; String resultValue = (percentage>=60)? "A grade":((percentage>=40)?"B grade":"Not Eligible"); System.out.println(resultValue); } }
3. Using the ternary operator as an alternative to switch-case
public class TernaryOperatorDemo5{ public static void main(String[] args) { int colorCode = 101; String color = null; switch(colorCode) { case 100 : color = "Yellow"; break; case 101 : color = "Green"; break; case 102 : color = "Red"; break; default : color = "Invalid"; } System.out.println("Color --->"+color); } }
public class TernaryOperatorDemo6{ public static void main(String[] args) { int colorCode = 101; String color = null; color=(colorCode==100)? "Yellow":((colorCode==101)?"Green":((colorCode==102)?"Red":"Invalid")); System.out.println("Color --->"+color); } }
4. Defining a ternary operator in Java
resultValue = testConditionStatement ? value1 : value2;
5. Writing a ternary condition in Java
resultValue = testConditionStatement ? value1 : value2; String result = (-2>2) ? “yes” : “no”;
6. Nested Ternary Operators
class Main { public static void main(String[] args) { // create a variable int n1 = 2, n2 = 9, n3 = -11; // nested ternary operator // to find the largest number int largest = (n1 >= n2) ? ((n1 >= n3) ? n1 : n3) : ((n2 >= n3) ? n2 : n3); System.out.println("Largest Number: " + largest); } }
7. Replacing a simple if-else statement with a ternary operator
class Main { public static void main(String[] args) { // create a variable int number = 24; if(number > 0) { System.out.println("Positive Number"); } else { System.out.println("Negative Number"); } } }
class Main { public static void main(String[] args) { // create a variable int number = 24; String result = (number > 0) ? "Positive Number" : "Negative Number"; System.out.println(result); } }
8. Evaluating An Expression with a ternary operator
public void whenConditionIsTrue_thenOnlyFirstExpressionIsEvaluated() { int exp1 = 0, exp2 = 0; int result = 12 > 10 ? ++exp1 : ++exp2; assertThat(exp1).isEqualTo(1); assertThat(exp2).isEqualTo(0); assertThat(result).isEqualTo(1);
}
9. Nesting a ternary operator
String msg = num > 10 ? "Number is greater than 10" : num > 5 ? "Number is greater than 5" : "Number is less than equal to 5";
10. Nested Ternary Operator
String msg = num > 10 ? "Number is greater than 10" : (num > 5 ? "Number is greater than 5" : "Number is less than equal to 5");
- How to swap two numbers without using a temp variable? (solution)
- How to solve the Producer-Consumer Problem in Java. (solution)
- How to find if the number is even or odd? (solution)
- How to find the Fibonacci sequence up to a given number? (solution)
- Write a method to remove duplicates from ArrayList in Java? (Solution)
- How to find Greatest Common Divisor of two numbers in Java? (solution)
- 10 Courses to Crack any Programming interview (courses)
- How to check if LinkedList contains any cycle in Java? (solution)
- How to check if a number is Armstrong's number or not? (solution)
- How do find the largest and smallest number in an array? (solution)
- How to add two numbers without using the + operator in Java? (solution)
- How to find prime factors of an integer in Java? (solution)
- How to check if an integer is a power of two in Java? (solution)
- How to find the first non-repeated characters from String in Java? (program)
No comments:
Post a Comment
Feel free to comment, ask questions if you have any doubt.