J2SE synthesis: The pain of using conditional operators

Source: Internet
Author: User
Tags expression

First, the case number:

u02c09

Second, the case name:

The pains of using conditional operators

Third, the key words:

Conditional operators, three-mesh operators, ternary operators, type conversions

Four, the case scenario description:

The conditional operator has three operands. It is in the form of: BOOLEAN-EXP?EXP1:EXP2. It evaluates the Boolean expression first, and if true, the value of the entire expression is the value of the expression Exp1, and if the Boolean expression evaluates to False, then the value of the entire expression is the EXP2 value.

intk= (2>3) 23:45; Because the Boolean expression 2>3 is False, the value of K is 45.

All of these are about determining the value of the conditional operator, so how is the type of the conditional operator value determined? Look at the following code:

(1)

charx= ' A '; Line1

inti=0; Line2

System.out.println (true?x:0); Line3

System.out.println (true?x:1111111110); Line4

System.out.println (FALSE?I:X); Line5

(2)

/*

* Assume that test is a class that has already been defined

*/

Testt=flase?newobject (): Newtest ();

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.