Java question mark? Operator usage, question mark expression

Source: Internet
Author: User

 

Java question mark? Operator usage

* Java provides a special ternary operator that is often used to replace an if-then-else statement of a certain type.
This operator is? It seems a little confused at first

But once you have mastered it, use? Operators are convenient and efficient.
*? The general format of operators is as follows:

Expression1? Expression2: expression3

Expression1 is a Boolean expression. If expression1 is true, expression2 is evaluated;
Otherwise, expression3 is evaluated. Whole? The expression value is the value of the evaluated expression (expression2 or expression3.
Expression2 and expression3 are expressions of any type except void, and they must be of the same type.

String type = NULL = ""? "": "1 ";

Type = 1 in this example;

Because null = "" is false, the value of the following expression is 1,

The value of type is 1;

? The result of the expression must be a value (or a value obtained after an operation). It is not a simple operation. For example, the following operations are incorrect.

Int A = 1;
Int B = 1;
A = B? System. Out. println ("A = B correct"): system. Out. println ("A = B incorrect ");

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.