An explanation of ternary operators in Java

Source: Internet
Author: User

Recently in the lead interns encountered a lot of novice asked about the three-operator Java topics, most of the code results, a few of the application problems. In view of a lot of data on the Java ternary operation is too simple, the information on the Internet and the topic is not very perfect, the results of the analysis of the answer is different, so this summary, of course, only for personal views, the level of limited, insufficient, but also please point out that the exchange of learning with each other.

What is a Java ternary operator? There is no doubt that its operand has three, the first is a conditional expression, the remaining two is a value, the conditional expression is true when the operation takes the first value, the second value is taken as false.

The sample code is as follows: Boolean a = < 45? True:false the precedence of the ternary operator is higher than the assignment operator, so the value of the expression is true and the first true,a is true.

The ternary operator is used to determine that its equivalent If-else statement is as follows:
Boolean A; Declaring a Boolean variable
if (20<45)//20<45 as a condition of judgment
A = true; Conditional setting True assigns a value to a
"Else"
A = false;

In many cases, ternary operators can be interchanged with if-else statements, both of which can be equivalent to the effect of judgment. But the ternary operator and the If-else statement also have different, the following concrete talk about the differences.

First, there are different requirements for the return value, the ternary operator must have a return value requirement, after which the operation must have a result returned for the program developer to use, and the If-else statement does not necessarily have a return value, The result of the execution may be an assignment statement or a printout of the statement (which we all understand well).

Second, the performance of the two is different, the operation performance of the ternary operator is higher than the If-else statement, but it can be neglected in some small applets development and daily learning development.

Thirdly, the use of the scope of the two are different, in the case of some dynamic execution of ireport, can only use the ternary operator instead of the If-else statement, of course, in most cases the two can be equivalently interchangeable.

In the four, the linguistic characteristics of the two are different, and the ternary operator relates to a language characteristic that becomes the binocular value enhancement (binary numeric promotion). The so-called binocular value enhancement, in the three-mesh operator Java development environment can be easily understood as the binocular operator type conversion problem (easy to understand). The specific rules are summarized as follows:

1) If a variable of the data type is defined with the value of an undefined variable to participate in the binocular operation of the ternary operator, then the returned result is a large range (high precision) type

2) If the two variables that define the data type participate in the second-binocular operation of the ternary operator, the returned result is a large range (high precision) type
3) If the direct numerical comparison, will automatically transform into a large range (high accuracy) data type

The JVM, when assigning data types to a value, chooses a data type that is exactly the size of the data to be allocated (99.0 is float,99 to Byte/short), and the common data types in Java range from small to large (precision from high to low): Byte<short <char<int<float<double. Here is a piece of code to explain the above three rules, the following test code also covers a lot of interview topics, I hope you can understand the above three rules.

A= Char ' a ';
i=96 int;
03//Rule 1, defines the data type of the variable and the value of the undefined variable, the result is automatically converted to a high precision
System.out.println (2==2?i:9.0);
//JVM the data type assigned to the value, 98 is not of type int, but byte/short, so the result becomes ASCII code 98

06//corresponding characters
System.out.println (2==2?98:A);
08//Rule 2, two variables that already have a data type defined, the result is automatically converted to a high-precision
System.out.println (2==2?A:I);
10//Rule 3, two undefined values, the result is automatically converted to a high precision
System.out.println (2==2?99:9.0);
System.out.println (2==2?99: ' B ');

The result of the above code is: 96.0
B
97
99.0
C

You can refer to the above code and the results understand the three-dimensional operator of the binocular value of the problem, because I have limited level, I hope this article can bring a certain help and enlightenment, but also hope that you are not hesitate to enlighten us, put forward your valuable suggestions.

This article is from the "Daydream Blue" blog, so be sure to keep this source http://itmlt1029.blog.51cto.com/10658744/1689127

An explanation of ternary operators in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.