Java-preliminary Understanding-Chapter III-Ternary operators

Source: Internet
Author: User

I. Overview

The ternary operator is the operator that participates in the operation of three elements.

Ternary operators have specific symbolic and expressive forms, (conditional expressions)? Expression 1: Expression 2; According to whether the result of the conditional expression is true or false,

To decide whether to run an expression 1 or 2.

Let's start with a demonstration of the ternary operator:

A DOS display error occurred during compilation. The first mistake is to explain that there are errors in the grammar, and that a fault occurs, resulting in a subsequent series of errors.

When the source program is changed, DOS is displayed as normal.

I changed another way of writing, and the DOS turned out to be wrong.

All of these errors show that we are unfamiliar with the understanding and use of ternary operators.

Two. The ternary operator

Analysis: What is a variable? Is the data that is uncertain. If you are not sure, it is a variable, you need to define the type, the beginning of the assignment can not be assigned, but the output must have a numeric value, see. As long as the output is not Z, then even if z is not assigned, the source program can be compiled, run.

it the data in Max is not deterministic, so it is also a variable and needs to define the type. If you do not define a type, DOS will error, shown below. That is, as long as it is a variable, be sure to define the type, assigning no value can be said otherwise.

Here is a correct writing.

Summary: I always feel that the examples explained here are too thin to explain the profound meaning of the use of ternary operators.

(conditional expression)? Expression 1: Expression 2; The conditional expression here is not a simple comparison size, and for how to write an expression,

We are also not clear. do you want to have the style of the variable assigned to it on the left side of the conditional expression ? Based on the instructions above,

One thing we can be certain of is that the result of this ternary operator must be a variable. Do you have to find a space to install it?

One more thing: as long as it's an operator, there's always a result.

From this comparison the three number is the largest, and the above comparison two is the largest, we go back to the original sentence, the computer is to help calculate the tool,

The program shows a lot of work in our daily life on the computer. We only need to talk about the prior knowledge points, through the computer advanced

The way the language is written, it can be written on a computer. This requires us to be very familiar with the writing rules of computer language.

Java-preliminary Understanding-Chapter III-Ternary operators

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.