Puzzle 8: Dos Equis

Source: Internet
Author: User

This puzzle will test your understanding of the conditional operator, which has a wider name: the question mark colon operator. What will the following program print?

public class DosEquis{    public static void main(String[] args){        char x = ‘X’;        int i = 0;        System.out.println(true ? x : 0);         System.out.println(false ? i : x);     }}

This program consists of two variable statements and two print statements. The first print statement calculates the condition expression (true? X: 0) and print the result. The result is the 'X' value of char type variable X '. The second print statement calculates the expression (false? I: X) and print the result. The result is still 'X' X, so this program should print xx. However, if you run the program, you will find that it prints x88. This behavior looks strange. The first print statement prints X, while the second prints 88. What are their different behaviors?

The answer lies in the dark corner of the normative conditional expression section. Note that in these two expressions, the type of the second and third operands of each expression is different: X is of the char type, and 0 and I are of the int type. As mentioned in the solution to puzzle 5, computation of the hybrid type can cause confusion, which is more obvious than in conditional expressions than in any other place. You may have considered that the result types of the two conditional expressions in this program are the same, just as their operand types are the same, even though the order of the operands is reversed, however, this is not the case.

The rules for determining the result type of conditional expressions are too lengthy and complex, so it is difficult to fully remember them, but the core is the following three points:

1. If the second and third operands have the same type, it is the type of the conditional expression. In other words, you can avoid great troubles by bypassing the computation of the hybrid type.

2. If the type of an operand is t, t indicates byte, short, or char, And the other operand is a constant expression of the int type, its value can be represented by the type T, the type of the conditional expression is T.

3. Otherwise, binary numbers are used for the operand type, and the conditional expression type is the type after the second and third operands are upgraded.

2 and 3 are key to this puzzle. In the two conditional expressions of the program, the type of one operand is Char, and the type of the other is int. In both expressions, the int operand is 0, which can be expressed as a char. However, only the int operands in the first expression are constants (0), while the int operands in the second expression are variables (I ). Therefore, point 2nd is applied to the first expression. The returned type is Char, and point 3rd is applied to the second expression, the returned type is the type after the binary numbers are used for int and char, that is, Int.

The type of the conditional expression determines which overloaded print method will be called. For the first expression, printstream. Print (char) will be called, and for the second expression, printstream. Print (INT) will be called. The previous overload method prints the value of variable X as a Unicode character (x), and the latter overload method prints the value as a decimal INTEGER (88. So far, the puzzle has been solved.

In short, it is usually better to use the second and third operands of the same type in the condition expression. Otherwise, you and your program readers must thoroughly understand the complex rules of these expressions.

For Language designers, it may be possible to design a conditional operator that sacrifices some flexibility but adds simplicity. For example, it seems reasonable to require that the second and third operands have the same type. Alternatively, the conditional operator can be defined as having no special processing on constants. To make these choices more acceptable to programmers, you can provide the syntax to represent all original types of literal constants. This may indeed be a good note, because it increases language consistency and completeness, while reducing the need for transformation.

Puzzle 8: Dos Equis

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.