In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
Today, I would like to share with you what the Java ternary operator is and how to use the relevant knowledge points, detailed content, clear logic, I believe that most people still know too much about this knowledge, so share this article for your reference, I hope you can get something after reading this article, let's take a look at it.
Java provides a special ternary operator (also known as the ternary operator) that is often used to replace a certain type of if-then-else statement. The symbol of the conditional operator is represented as "?:", and three operands are required to use it, so it is called the 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 you can use this conditional operator to implement a simple two-branch program.
The following is an example of using the conditional operator.
Intx,y,z
Xerox, 6, and 2.
Zhonx > y?x-y:x+y
To calculate the value of z here, we must first determine the value expressed by x > y, if it is true,z, the value is xmury; otherwise, the value of z is xmury. It is obvious that the x > y expression results in true, so the value of z is 4.
Tip: you can understand the conditional operator as a simplified form of if-else statements, and 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.
For example, the following expression:
X > y?x-=y:x+=y
Syntax errors occur at compile time because the conditional operator takes precedence over the assignment operator, and the above statement is actually equivalent to:
(X > y?x-=y:x) + = y
The operator "+ =" is the assignment operator, which requires that the left Operand should be a variable, so an error occurs. To avoid such errors, you can use parentheses "0" to distinguish them. For example, the following is the correct expression.
(X > y)? (xmurf / y): (x / y / y)
Example 1
Declare three variables x, y, z in the program, and let the user enter the value of x from the keyboard, and then use the conditional operator to assign values to the variables y and z. The implementation code is as follows:
PublicclassTest9 {
Publicstaticvoidmain (String [] args) {
Intx,y,z;// declares three variables
System.out.print ("Please enter a number:")
Scannerinput=newScanner (System.in)
X=input.nextInt (); / / the user enters the value of x
/ / determine whether the value of x is greater than 5. If it is yroomx, otherwise it is yawmerx.
Yellowx > 5?x:-x
/ / determine whether the value of y is greater than x, and if it is zroomy, otherwise zroom5
Zhony > x?y:5
System.out.printf ("x% d\ n", x)
System.out.printf ("yawning% d\ n", y)
System.out.printf ("zonal% 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 if the condition is true, then the value of y is x, that is, y is 58. Then it is determined whether the value of y is greater than x, because both the value of y and the value of x are 58, so this condition is not true. Enter the value of x as 4 again, and then determine whether the value of x is greater than 5, and then determine whether the value of y is greater than x, and then determine whether the value of y is greater than x, then zonal 5.
These are all the contents of the article "what is the Java ternary operator and how to use it?" Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.