System.out.println(true?false:true == true?false:true);输出是什么呢??并给出解释

解决方案 »

  1.   

    输出false啊。
    条件表达式 [A]?b:c
    若A成立true,则返回b,否则返回c
    true?false:true == true?false:true
    先判断第一个true结果true,返回第一个false,后面的不执行。
      

  2.   

    true?false:true这个为false
    再判断false == true?false:true
    当然输出false了
      

  3.   

    int i=0;
    System.out.println(ttrue?"11":true == true?i++:i--)  //11System.out.println(i)  //0  i++ i--未执行。问题的确无聊!呵呵
      

  4.   

    System.out.println(true?false:true == true?false:true);没什么可以解释的。只要了解三元运算符就可以知道,这段代码要么编译不通过,否则,只可能进第一个问号后面,判断为真的部分,冒号后面,直接无视等价于
    X=true
    Y=false
    Z=true == true?false:true
    System.out.println(X?Y:Z);
      

  5.   

    System.out.println(true?false:(true == true?false:true));
      

  6.   

    这是跟操纵符的优先级有关的。 看下表:
    <table border="1">
        <tr>      <th>Priority</th>
          <th>Operator</th>
          <th>Operation</th>
          <th>Order of Evaluation</th>
        </tr>    <tr>      <td rowspan="3">1</td>
          <td><code>[ ]</code></td>
          <td>Array index</td>
          <td rowspan="3">Left to Right</td>
        </tr>
        <tr>
          <td><code>()</code></td>      <td>Method call</td>
        </tr>
        <tr>
          <td><code>.</code></td>
          <td>Member access</td>
        <tr>
          <td rowspan="7">2</td>      <td><code>++</code></td>
          <td>Prefix or postfix increment</td>
          <td rowspan="7">Right to Left</td>
        </tr>
        <tr>
          <td><code>--</code></td>
          <td>Prefix or postfix decrement</td>    </tr>
        <tr>
          <td><code>+ -</code></td>
          <td>Unary plus, minus</td>
        </tr>
        <tr>
          <td><code>~</code></td>      <td>Bitwise NOT</td>
        </tr>
        <tr>
          <td><code>!</code></td>
          <td>Boolean (logical) NOT</td>
        </tr>
        <tr>      <td><code>(type)</code></td>
          <td>Type cast</td>
        </tr>
        <tr>
          <td><code>new</code></td>
          <td>Object creation</td>
        <tr>      <td rowspan="1">3</td>
          <td><code>* / %</code></td>
          <td>Multiplication, division, remainder</td>
          <td rowspan="1">Left to Right</td>
        <tr>
          <td rowspan="2">4</td>      <td><code>+ -</code></td>
          <td>Addition, subtraction</td>
          <td rowspan="2">Left to Right</td>
        </tr>
        <tr>
          <td><code>+</code></td>
          <td>String concatenation</td>    <tr>
          <td rowspan="3">5</td>
          <td><code>&lt;&lt;</code></td>
          <td>Signed bit shift left to right</td>
          <td rowspan="3">Left to Right</td>
        </tr>
        <tr>      <td><code>&gt;&gt;</code></td>
          <td>Signed bit shift right to left</td>
        </tr>
        <tr>
          <td><code>&gt;&gt;&gt;</code></td>
          <td>Unsigned bit shift right to left</td>
        <tr>
          <td rowspan="3">6</td>      <td><code>&lt; &lt;=</code></td>
          <td>Less than, less than or equal to</td>
          <td rowspan="3">Left to Right</td>
        </tr>
        <tr>
          <td><code>&gt; &gt;=</code></td>      <td>Greater than, greater than or equal to</td>
        </tr>
        <tr>
          <td><code>instanceof</code></td>
          <td>Reference test</td>
        <tr>
          <td rowspan="2">7</td>      <td><code>==</code></td>
          <td>Equal to</td>
          <td rowspan="2">Left to Right</td>
        </tr>
        <tr>
          <td><code>!=</code></td>
          <td>Not equal to</td>    <tr>
          <td rowspan="2">8</td>
          <td><code>&amp;</code></td>
          <td>Bitwise AND</td>
          <td rowspan="2">Left to Right</td>
        </tr>
        <tr>      <td><code>&amp; </code></td>
          <td>Boolean (logical) AND</td>
        <tr>
          <td rowspan="2">9</td>
          <td><code>^</code></td>
          <td>Bitwise XOR</td>      <td rowspan="2">Left to Right</td>
        </tr>
        <tr>
          <td><code>^ </code></td>
          <td>Boolean (logical) XOR</td>
        <tr>
          <td rowspan="2">10</td>      <td><code>|</code></td>
          <td>Bitwise OR</td>
          <td rowspan="2">Left to Right</td>
        </tr>
        <tr>
          <td><code>| </code></td>
          <td>Boolean (logical) OR</td>    <tr>
          <td rowspan="1">11</td>
          <td><code>&amp;&amp;</code></td>
          <td>Boolean (logical) AND</td>
          <td rowspan="1">Left to Right</td>
        <tr>
          <td rowspan="1">12</td>      <td><code>||</code></td>
          <td>Boolean (logical) OR</td>
          <td rowspan="1">Left to Right</td>
        <tr>
          <td rowspan="1">13</td>
          <td><code>? :</code></td>      <td>Conditional</td>
          <td rowspan="1">Right to Left</td>
        <tr>
          <td rowspan="2">14</td>
          <td><code>=</code></td>
          <td>Assignment</td>      <td rowspan="2">Right to Left</td>
        </tr>
        <tr>
          <td><code>*= /= += -= %= <br />
         &lt;&lt;= &gt;&gt;= &gt;&gt;&gt;= <br />
         &amp;= ^= |=</code></td>      <td>Combinated assignment <br />
           (operation and assignment)</td>
    </table>
    从这张表我们可以看出, ==的优先级比条件操纵符高,所以
    true?false:(true == true?false:true)(true == true?false:true) 的结果是 false
    因为 true?false:true 得出的结果是false, 然后true == false 就是 false了那么,现在的结果就是: true?false:false因为这个句子的条件永远都是true (不是true的话也得出相同的答案)。我们可以通过下面的一个句子来证实:
    false?false:true == true?false:true
    它的结果和我们的假设是一样的。
    现在我们来看看下面,我改过后的句型:
    (true?false:true) == (true?false:true)
    结果是: true因为我们加了括号,而括号的优先级是很高的(如表所示),所以
    两边的true?false:true会先被计算出来,也就是:
    false == false
    那么,结果就很明显了,是ture.
      

  7.   

    刚刚没放上表格。Java操纵符,优先级表(英文):
    http://www.java-tips.org/java-se-tips/java.lang/what-is-java-operator-precedence.html