⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tij305.htm

📁 这也是我们java老师给我们的thinking in java的一些资料
💻 HTM
📖 第 1 页 / 共 5 页
字号:
    <font color=#009900>// Treating an int as a boolean is not legal Java:</font>
<font color=#009900>//! System.out.println("i &amp;&amp; j is " + (i &amp;&amp; j));</font>
<font color=#009900>//! System.out.println("i || j is " + (i || j));</font>
<font color=#009900>//! System.out.println("!i is " + !i);</font>
    System.out.println(<font color=#004488>"(i &lt; 10) &amp;&amp; (j &lt; 10) is "</font>
       + ((i &lt; 10) &amp;&amp; (j &lt; 10)) );
    System.out.println(<font color=#004488>"(i &lt; 10) || (j &lt; 10) is "</font>
       + ((i &lt; 10) || (j &lt; 10)) );
    monitor.expect(<font color=#0000ff>new</font> String[] {
      <font color=#004488>"%% i = -?\\d+"</font>,
      <font color=#004488>"%% j = -?\\d+"</font>,
      <font color=#004488>"%% i &gt; j is (true|false)"</font>,
      <font color=#004488>"%% i &lt; j is (true|false)"</font>,
      <font color=#004488>"%% i &gt;= j is (true|false)"</font>,
      <font color=#004488>"%% i &lt;= j is (true|false)"</font>,
      <font color=#004488>"%% i == j is (true|false)"</font>,
      <font color=#004488>"%% i != j is (true|false)"</font>,
      <font color=#004488>"%% \\(i &lt; 10\\) &amp;&amp; \\(j &lt; 10\\) is (true|false)"</font>,
      <font color=#004488>"%% \\(i &lt; 10\\) \\|\\| \\(j &lt; 10\\) is (true|false)"</font>
    });
  }
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE><p><br></p>
<p>In the regular expressions in the <b>expect(&#160;)</b> statement, parentheses have the effect of grouping an expression, and the vertical bar &#145;<b>|</b>&#146; means OR. So:<br></p>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>(<font color=#0000ff>true</font>|<font color=#0000ff>false</font>)</PRE></FONT></BLOCKQUOTE><p><br></p>
<p>Means that this part of the string may be either &#145;true&#146; or &#145;false&#146;. Because these characters are special in regular expressions, they must be escaped with a &#145;<b>\\</b>&#146; if you want them to appear as ordinary characters in the expression. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]A0455" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>You can apply AND, OR, or NOT to <b>boolean</b> values only. You can&#146;t use a non-<b>boolean</b> as if it were a <a name="Index207"></a><b>boolean</b> in a logical expression as you can in C and C++. You can see the failed attempts at doing this commented out with a <b>//!</b> comment marker. The subsequent expressions, however, produce <b>boolean</b> values using relational comparisons, then use logical operations on the results. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap03_537" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>Note that a <b>boolean</b> value is automatically converted to an appropriate text form if it&#146;s used where a <b>String</b> is expected. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap03_538" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>You can replace the definition for <b>int</b> in the preceding program with any other primitive data type except <b>boolean</b>. Be aware, however, that the comparison of floating-point numbers is very strict. A number that is the tiniest fraction different from another number is still &#147;not equal.&#148; A number that is the tiniest bit above zero is still nonzero. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap03_539" title="Send BackTalk Comment">Feedback</a></font><br></p>
<h4>
<a name="Index208"></a><a name="Heading2070"></a>Short-circuiting</h4>
<p>When dealing with logical operators, you run into a phenomenon called &#147;short circuiting.&#148; This means that the expression will be evaluated only <a name="Index209"></a><a name="Index210"></a><a name="Index211"></a><i>until</i> the truth or falsehood of the entire expression can be unambiguously determined. As a result, the latter parts of a logical expression might not be evaluated. Here&#146;s an example that demonstrates short-circuiting:<br></p>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c03:ShortCircuit.java</font>
<font color=#009900>// Demonstrates short-circuiting behavior.</font>
<font color=#009900>// with logical operators.</font>
<font color=#0000ff>import</font> com.bruceeckel.simpletest.*;

<font color=#0000ff>public</font> <font color=#0000ff>class</font> ShortCircuit {
  <font color=#0000ff>static</font> Test monitor = <font color=#0000ff>new</font> Test();
  <font color=#0000ff>static</font> <font color=#0000ff>boolean</font> test1(<font color=#0000ff>int</font> val) {
    System.out.println(<font color=#004488>"test1("</font> + val + <font color=#004488>")"</font>);
    System.out.println(<font color=#004488>"result: "</font> + (val &lt; 1));
    <font color=#0000ff>return</font> val &lt; 1;
  }
  <font color=#0000ff>static</font> <font color=#0000ff>boolean</font> test2(<font color=#0000ff>int</font> val) {
    System.out.println(<font color=#004488>"test2("</font> + val + <font color=#004488>")"</font>);
    System.out.println(<font color=#004488>"result: "</font> + (val &lt; 2));
    <font color=#0000ff>return</font> val &lt; 2;
  }
  <font color=#0000ff>static</font> <font color=#0000ff>boolean</font> test3(<font color=#0000ff>int</font> val) {
    System.out.println(<font color=#004488>"test3("</font> + val + <font color=#004488>")"</font>);
    System.out.println(<font color=#004488>"result: "</font> + (val &lt; 3));
    <font color=#0000ff>return</font> val &lt; 3;
  }
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> main(String[] args) {
    <font color=#0000ff>if</font>(test1(0) &amp;&amp; test2(2) &amp;&amp; test3(2))
      System.out.println(<font color=#004488>"expression is true"</font>);
    <font color=#0000ff>else</font>
      System.out.println(<font color=#004488>"expression is false"</font>);
    monitor.expect(<font color=#0000ff>new</font> String[] {
      <font color=#004488>"test1(0)"</font>,
      <font color=#004488>"result: true"</font>,
      <font color=#004488>"test2(2)"</font>,
      <font color=#004488>"result: false"</font>,
      <font color=#004488>"expression is false"</font>
    });
  }
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE><p><br></p>
<p>Each test performs a comparison against the argument and returns true or false. It also prints information to show you that it&#146;s being called. The tests are used in the expression: <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap03_540" title="Send BackTalk Comment">Feedback</a></font><br></p>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#0000ff>if</font>(test1(0) &amp;&amp; test2(2) &amp;&amp; test3(2))</PRE></FONT></BLOCKQUOTE><p><br></p>
<p>You might naturally think that all three tests would be executed, but the output shows otherwise. The first test produced a <b>true</b> result, so the expression evaluation continues. However, the second test produced a <b>false</b> result. Since this means that the whole expression must be <b>false</b>, why continue evaluating the rest of the expression? It could be expensive. The reason for short-circuiting, in fact, is that you can get a potential performance increase if all the parts of a logical expression do not need to be evaluated. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap03_542" title="Send BackTalk Comment">Feedback</a></font><br></p>
<h3>
<a name="_Toc375545254"></a><a name="_Toc24775578"></a><a name="Heading2113"></a>Bitwise
operators</h3>
<p><a name="Index212"></a><a name="Index213"></a>The bitwise operators allow you to manipulate individual bits in an integral primitive data type. Bitwise operators perform Boolean algebra on the corresponding bits in the two arguments to produce the result. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap03_543" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p><a name="Index214"></a>The bitwise operators come from C&#146;s low-level orientation, where you often manipulate hardware directly and must set the bits in hardware registers. Java was originally designed to be embedded in TV set-top boxes, so this low-level orientation still made sense. However, you probably won&#146;t use the bitwise operators much. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap03_544" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p><a name="Index215"></a>The bitwise AND operator (<b>&amp;</b>)<a name="Index216"></a><a name="Index217"></a> produces a one in the output bit if both input bits are one, otherwise it produces a zero. The bitwise OR operator (<b>|</b>)<a name="Index218"></a><a name="Index219"></a> produces a one in the output bit if either input bit is a one and produces a zero only if both input bits are zero. The bitwise EXCLUSIVE OR, or XOR (<b>^</b>),<a name="Index220"></a><a name="Index221"></a><a name="Index222"></a> produces a one in the output bit if one or the other input bit is a one, but not both. The bitwise NOT (<a name="Index223"></a><b>~</b>, also called the <i>ones complement </i>operator) is a unary operator; it takes only one argument. (All other bitwise operators are binary operators.) Bitwise NOT produces the opposite of the input bit&#151;a one if the input bit is zero, a zero if the input bit is one. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap03_545" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p><a name="Index224"></a><a name="Index225"></a><a name="Index226"></a><a name="Index227"></a><a name="Index228"></a><a name="Index229"></a>The bitwise operators and logical operators use the same characters, so it is helpful to have a mnemonic device to help you remember the meanings: because bits are &#147;small,&#148; there is only one character in the bitwise operators. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap03_546" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>Bitwise operators can be combined with the <b>=</b> sign to unite the operation and assignment: <b>&amp;=</b>, <a name="Index230"></a><b>|=</b><a name="Index231"></a> and <b>^=</b><a name="Index232"></a> are all legitimate. (Since <b>~</b> is a unary operator, it cannot be combined with the <b>=</b> sign.) <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap03_547" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>The <b>boolean</b> type is treated as a one-bit value, so it is somewhat different. You can perform a bitwise AND, OR, and XOR, but you can&#146;t perform a bitwise NOT (presumably to prevent confusion with the logical NOT). For <b>boolean</b>s, the bitwise operators have the same effect as the logical operators except that they do not short circuit. Also, bitwise operations on <b>boolean</b>s include an XOR logical operator that is not included under the list of &#147;logical&#148; operators. You&#146;re prevented from using <b>boolean</b>s in shift expressions, which are described next. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap03_548" title="Send BackTalk Comment">Feedback</a></font><br></p>
<h3>
<a name="_Toc375545255"></a><a name="_Toc24775579"></a><a name="Heading2120"></a>Shift
operators</h3>
<p><a name="Index233"></a><a name="Index234"></a>The shift operators also manipulate bits. They can be used solely with primitive, integral types. The left-shift operator (<b>&lt;&lt;</b>)<a name="Index235"></a><a name="Index236"></a> produces the operand to the left of the operator shifted to the left by the number of bits specified after the operator (inserting zeroes at the lower-order bits). The signed right-shift operator (<b>&gt;&gt;</b>)<a name="Index237"></a><a name="Index238"></a> produces the operand to the left of the operator shifted to the right by the number of bits specified after the operator. The signed right shift <b>&gt;&gt; </b>uses <i>sign extension</i><a name="Index239"></a><a name="Index240"></a>: if the value is positive, zeroes are inserted at the higher-order bits; if the value is negative, ones are inserted at the higher-order bits. Java has also added the unsigned right shift <b>&gt;&gt;&gt;, </b>which<b> </b>uses <i>zero extension</i><a name="Index241"></a><a name="Index242"></a>: regardless of the sign, zeroes are inserted at the higher-order bits.<i> </i>This operator does not exist in C or C++. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap03_549" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>If you shift a <b>char</b>, <b>byte,</b> or <b>short</b>, it will be promoted to <b>int</b> before the shift takes place, and the result will be an <b>int</b>. Only the five low-order bits of the right-hand side will be used. This prevents you from shifting more than the number of bits in an <b>int</b>. If you&#146;re operating on a <b>long</b>, you&#146;ll get a <b>long</b> result. Only the six low-order bits of the right-hand side will be used, so you can&#146;t shift more than the number of bits in a <b>long</b>. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap03_550" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>Shifts can be combined with the equal sign (<b>&lt;&lt;=</b> or <b>&gt;&gt;=</b> or <b>&gt;&gt;&gt;=</b>). The lvalue is replaced by the lvalue shifted by the rvalue. There is a problem, however, with the unsigned right shift combined with assignment. If you use it with <a name="Index243"></a><a name="Index244"></a><b>byte</b> or <b>short</b>, you don&#146;t get the correct results. Instead, these are promoted to <b>int</b> and right shifted, but then truncated as they are assigned back into their variables, so you get <b>-1</b> in those cases. The following example demonstrates this: <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap03_551" title="Send BackTalk Comment">Feedback</a></font><br></p>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c03:URShift.java</font>
<font color=#009900>// Test of unsigned right shift.</font>
<font color=#0000ff>import</font> com.bruceeckel.simpletest.*;

<font color=#0000ff>public</font> <font color=#0000ff>class</font> URShift {
  <font color=#0000ff>static</font> Test monitor = <font color=#0000ff>new</font> Test();
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> main(String[] args) {
    <font color=#0000ff>int</font> i = -1;
    System.out.println(i &gt;&gt;&gt;= 10);
    <font color=#0000ff>long</font> l = -1;
    System.out.println(l &gt;&gt;&gt;= 10);
    <font color=#0000ff>short</font> s = -1;
    System.out.println(s &gt;&gt;&gt;= 10);
    <font color=#0000ff>byte</font> b = -1;
    System.out.println(b &gt;&gt;&gt;= 10);
    b = -1;
    System.out.println(b&gt;&gt;&gt;10);
    monitor.expect(<font color=#0000ff>new</font> String[] {
      <font color=#004488>"4194303"</font>,
      <font color=#004488>"18014398509481983"</font>,
      <font color=#004488>"-1"</font>,
      <font color=#004488>"-1"</font>,
      <font color=#004488>"4194303"</font>
    });
  }
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE><p><br></p>
<p>In the last shift, the resulting value is not assigned back into <b>b</b>, but is printed directly, so the correct behavior occurs. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap03_552" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>Here&#146;s an example that demonstrates the use of all the operators involving bits:<br></p>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c03:BitManipulation.java</font>
<font color=#009900>// Using the bitwise operators.</font>
<font color=#0000ff>import</font> com.bruceeckel.simpletest.*;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -