Home  >  Article  >  Java  >  What is Java ternary operator and how to use it

What is Java ternary operator and how to use it

PHPz
PHPzforward
2023-04-19 10:04:082391browse

Java provides a special ternary operator (also called the ternary operator) that is often used to replace a certain type of if-then-else statement. The symbol of the conditional operator is "?:". When using this operator, three operands are required, so it is called a ternary operator. The general syntax structure for using conditional operators is:

result=

Where expression is a Boolean expression. When expression is true, statement1 is executed, otherwise statement3 is executed. This ternary operator requires a result to be returned, so to implement a simple binary program, you can use this conditional operator.

The following is an example of using conditional operators.

 intx,y,z;

 x=6,y=2;

 z=x>y?x-y:x y;

 Here To calculate the value of z, first determine the value expressed by x>y. If it is true, the value of z is x-y; otherwise, the value of z is x y. Obviously the x>y expression evaluates to true, so the value of z is 4.

Tips: The conditional operator can be understood as a simplified form of the if-else statement. When using simpler expressions, using this operator can simplify the program code and make the program more readable.

When using conditional operators, you should also pay attention to priority issues

For example, the following expression:

 x>y?x-=y:x =y;

A syntax error will occur during compilation because the conditional operator takes precedence over the assignment operator. The above statement is actually equivalent to:

 (x>y?x-=y:x) =y;

 The operator "=" is an assignment operator, which requires that the left operand should be a variable, so an error occurs. To avoid such errors, brackets "0" can be used to distinguish them. For example, the following is the correct expression.

 (x>y)?(x-=y):(x =y);

 Example 1

Declare three variables x, y, z, and the user enters the value of x from the keyboard, and then uses conditional operators to assign values ​​to variables y and variables z. The implementation code is as follows:

publicclassTest9{

publicstaticvoidmain(String[]args){

intx,y,z;//Declare three variables

System.out.print("Please enter a number:");

Scannerinput=newScanner(System.in);

x=input.nextInt();//Input x by the user The value of

// Determine whether the value of x is greater than 5, if it is y=x, otherwise y=-x

 y=x>5?x:-x;

 //Determine whether the value of y is greater than x, if z=y, otherwise z=5

 z=y>x?y:5;

 System.out.printf(" x=%d\n",x);

 System.out.printf("y=%d\n",y);

 System.out.printf("z= %d\n",z);

 }

 }

In this program, first enter the value of x as 58, and then determine whether the value of x is greater than 5 , obviously the condition is true, then the value of y is x, that is, y=58. Then determine whether the value of y is greater than x. Because the value of y and the value of x are both 58, this condition is not true, then z=5. Enter the value of x again as 4, and then judge whether the value of x is greater than 5, and if it is not true, then y=-4; then judge whether the value of y is greater than x, and if it is not true, then z=5.

The above is the detailed content of What is Java ternary operator and how to use it. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete