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

📄 chap03.htm

📁 Thinking in Java, 2nd edition
💻 HTM
📖 第 1 页 / 共 5 页
字号:
Shortcuts can make code much easier to type, and either easier or harder to
read.  
</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER3_I26' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER3_I27>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Two of the nicer shortcuts are the
increment<A NAME="Index224"></A> and decrement<A NAME="Index225"></A> operators
(often referred to as the auto-increment<A NAME="Index226"></A> and
auto-decrement<A NAME="Index227"></A> operators). The decrement operator is
<B>--</B> and means &#8220;decrease by one unit.&#8221; The increment operator
is <B>++</B> and means &#8220;increase by one unit.&#8221; If <B>a</B> is an
<B>int</B>, for example, the expression <B>++a</B> is equivalent to (<B>a = a +
1</B>). Increment and decrement operators produce the value of the variable as a
result.  
</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER3_I27' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER3_I28>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">There are two versions of each type of
operator, often called the prefix and postfix versions. Pre-increment means the
<B>++ </B>operator appears before the variable or expression, and post-increment
means the <B>++</B> operator appears after the variable or expression.
Similarly, pre-decrement means the <B>-- </B>operator appears before the
variable or expression, and post-decrement means the <B>--</B> operator appears
after the variable or expression. For pre-increment and pre-decrement, (i.e.,
<B>++a</B> or <B>--a</B>), the operation is performed and the value is produced.
For post-increment and post-decrement (i.e. <B>a++ </B>or <B>a--</B>), the value
is produced, then the operation is performed. As an example:

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER3_I28' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER3_I29>
</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c03:AutoInc.java</font>
<font color=#009900>// Demonstrates the ++ and -- operators.</font>

<font color=#0000ff>public</font> <font color=#0000ff>class</font> AutoInc {
  <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;
    prt(<font color=#004488>"i : "</font> + i);
    prt(<font color=#004488>"++i : "</font> + ++i); <font color=#009900>// Pre-increment</font>
    prt(<font color=#004488>"i++ : "</font> + i++); <font color=#009900>// Post-increment</font>
    prt(<font color=#004488>"i : "</font> + i);
    prt(<font color=#004488>"--i : "</font> + --i); <font color=#009900>// Pre-decrement</font>
    prt(<font color=#004488>"i-- : "</font> + i--); <font color=#009900>// Post-decrement</font>
    prt(<font color=#004488>"i : "</font> + i);
  }
  <font color=#0000ff>static</font> <font color=#0000ff>void</font> prt(String s) {
    System.out.println(s);
  }
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The output for this program is:

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER3_I29' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER3_I30>
</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>i : 1
++i : 2
i++ : 2
i : 3
--i : 2
i-- : 2
i : 1</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">You can see that for the prefix form you
get the value after the operation has been performed, but with the postfix form
you get the value before the operation is performed. These are the only
operators (other than those involving assignment) that have side effects. (That
is, they change the operand rather than using just its
value.)<A NAME="Index228"></A>

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER3_I30' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER3_I31>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The increment operator is one explanation
for the name C++, implying &#8220;one step beyond C.&#8221; In an early Java
speech, <A NAME="Index229"></A>Bill Joy (one of the creators), said that
&#8220;Java=C++--&#8221; (C plus plus minus minus), suggesting that Java is C++
with the unnecessary hard parts removed and therefore a much simpler language.
As you progress in this book you&#8217;ll see that many parts are simpler, and
yet Java isn&#8217;t <I>that </I>much easier than <A NAME="Index230"></A>C++. 

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER3_I31' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER3_I32>
</FONT><A NAME="_Toc375545252"></A><A NAME="_Toc481064546"></A><BR></P></DIV>
<A NAME="Heading133"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Relational operators<BR><A NAME="Index231"></A><A NAME="Index232"></A></H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Relational operators generate a
<B>boolean</B> result. They evaluate the relationship between the values of the
operands. A relational expression produces <B>true</B> if the relationship is
true, and <B>false</B> if the relationship is untrue. The relational operators
are less than (&lt;)<A NAME="Index233"></A>, greater than
(&gt;)<A NAME="Index234"></A>, less than or equal to
(&lt;=)<A NAME="Index235"></A>, greater than or equal to
(&gt;=)<A NAME="Index236"></A>, equivalent (==)<A NAME="Index237"></A> and not
equivalent
(!=)<A NAME="Index238"></A>.<A NAME="Index239"></A><A NAME="Index240"></A><A NAME="Index241"></A><A NAME="Index242"></A><A NAME="Index243"></A><A NAME="Index244"></A>
Equivalence and nonequivalence works with all built-in data types, but the other
comparisons won&#8217;t work with type <B>boolean<A NAME="Index245"></A></B>.

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER3_I32' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER3_I33>
</FONT><BR></P></DIV>
<A NAME="Heading134"></A><FONT FACE = "Verdana"><H4 ALIGN="LEFT">
Testing object
equivalence<BR><A NAME="Index246"></A><A NAME="Index247"></A></H4></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The relational operators <B>==</B> and
<B>!=</B> also work with all objects, but their meaning often confuses the
first-time Java programmer. Here&#8217;s an example:

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER3_I33' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER3_I34>
</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c03:Equivalence.java</font>

<font color=#0000ff>public</font> <font color=#0000ff>class</font> Equivalence {
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> main(String[] args) {
    Integer n1 = <font color=#0000ff>new</font> Integer(47);
    Integer n2 = <font color=#0000ff>new</font> Integer(47);
    System.out.println(n1 == n2);
    System.out.println(n1 != n2);
  }
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The expression <B>System.out.println(n1
== n2)</B> will print the result of the <B>boolean</B> comparison within it.
Surely the output should be <B>true</B> and then <B>false</B>,<B> </B>since both
<B>Integer</B> objects are the same. But while the <I>contents</I> of the
objects are the same, the
<A NAME="Index248"></A><A NAME="Index249"></A>references are not the same and
the operators <B>==</B> and <B>!= </B>compare object references. So the output
is actually <B>false</B> and then <B>true</B>. Naturally, this surprises people
at first. 
</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER3_I34' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER3_I35>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">What if you want to compare the actual
contents of an object for equivalence? You must use the special method
<A NAME="Index250"></A><A NAME="Index251"></A><B>equals(&#160;)</B> that exists
for all objects (not <A NAME="Index252"></A>primitives, which work fine with
<B>==</B> and <B>!=</B>). Here&#8217;s how it&#8217;s used:

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER3_I35' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER3_I36>
</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c03:EqualsMethod.java</font>

<font color=#0000ff>public</font> <font color=#0000ff>class</font> EqualsMethod {
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> main(String[] args) {
    Integer n1 = <font color=#0000ff>new</font> Integer(47);
    Integer n2 = <font color=#0000ff>new</font> Integer(47);
    System.out.println(n1.equals(n2));
  }
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The result will be <B>true</B>, as you
would expect. Ah, but it&#8217;s not as simple as that. If you create your own
class, like this: 
</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER3_I36' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER3_I37>
</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c03:EqualsMethod2.java</font>

<font color=#0000ff>class</font> Value {
  <font color=#0000ff>int</font> i;
}

<font color=#0000ff>public</font> <font color=#0000ff>class</font> EqualsMethod2 {
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> main(String[] args) {
    Value v1 = <font color=#0000ff>new</font> Value();
    Value v2 = <font color=#0000ff>new</font> Value();
    v1.i = v2.i = 100;
    System.out.println(v1.equals(v2));
  }
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">you&#8217;re back to square one: the
result is <B>false</B>. This is because the default behavior of
<B>equals(&#160;)</B> is to compare references. So unless you <I>override</I>
<B>equals(&#160;)</B> in your new class you won&#8217;t get the desired
behavior. Unfortunately, you won&#8217;t learn about overriding until Chapter 7,
but being aware of the way <B>equals(&#160;)</B> behaves might save you some
grief in the meantime. 
</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER3_I37' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER3_I38>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Most of the Java library classes
implement <B>equals(&#160;)</B> so that it compares the contents of objects
instead of their references.

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER3_I38' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER3_I39>
</FONT><A NAME="_Toc375545253"></A><A NAME="_Toc481064547"></A><BR></P></DIV>
<A NAME="Heading135"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Logical operators<A NAME="Index253"></A><A NAME="Index254"></A></H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The logical operators AND
(&amp;&amp;)<A NAME="Index255"></A><A NAME="Index256"></A>, OR
(||)<A NAME="Index257"></A><A NAME="Index258"></A> and
<A NAME="Index259"></A><A NAME="Index260"></A>NOT (!) produce a <B>boolean</B>
value of <B>true</B> or <B>false</B>
<A NAME="Index261"></A><A NAME="Index262"></A>based on the logical relationship
of its arguments. This example uses the relational and logical operators:

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER3_I39' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER3_I40>
</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c03:Bool.java</font>
<font color=#009900>// Relational and logical operators.</font>
<font color=#0000ff>import</font> java.util.*;

<font color=#0000ff>public</font> <font color=#0000ff>class</font> Bool {
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> main(String[] args) {
    Random rand = <font color=#0000ff>new</font> Random();
    <font color=#0000ff>int</font> i = rand.nextInt() % 100;

⌨️ 快捷键说明

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