+1 vote
in JAVA by
Could you please explain Java Ternary Operator Examples and use of Java Ternary Operators

1 Answer

0 votes
by

Java Ternary Operator Examples

Java Programming Java8Java Technologies Object Oriented Programming

The ternary operator is also known as the conditional operator. This operator consists of three operands and is used to evaluate Boolean expressions. The goal of the operator is to decide, which value should be assigned to the variable. The operator is written as −

variable x = (expression) ? value if true: value if false

Example

Following is an example −

Online Demo

public class Test {

   public static void main(String args[]) {

      int a, b;

      a = 10;

      b = (a == 1) ? 20: 30;

      System.out.println( "Value of b is : " +  b );

      b = (a == 10) ? 20: 30;

      System.out.println( "Value of b is : " + b );

   }

}

Output

This will produce the following result −

Value of b is : 30

Value of b is : 20

Related questions

0 votes
asked Apr 30, 2021 in JAVA by rajeshsharma
0 votes
asked May 10, 2023 in Python Flask by john ganales
...