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

📄 tij0044.html

📁 学习java的经典书籍
💻 HTML
📖 第 1 页 / 共 5 页
字号:
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
modulus operator, when used with the result of the random number generator,
limits the result to an upper bound of the operand minus one (99 in this case).
</FONT><P></DIV>
<A NAME="Heading105"></A><H4 ALIGN=LEFT>
Unary
minus and plus operators
<P><A NAME="Index108"></A><A NAME="Index109"></A></H4>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
unary minus (-)<A NAME="Index110"></A><A NAME="Index111"></A>
and unary plus (+)<A NAME="Index112"></A><A NAME="Index113"></A>
are the same operators as binary minus and plus. The compiler figures out which
use is intended by the way you write the expression. For instance, the statement
</FONT><P></DIV><DIV ALIGN=LEFT><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">x
= -a;
</FONT></TT><P></DIV><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">has
an obvious meaning. The compiler is able to figure out:
</FONT><P></DIV><DIV ALIGN=LEFT><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">x
= a * -b;
</FONT></TT><P></DIV><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">but
the reader might get confused, so it is more clear to say:
</FONT><P></DIV><DIV ALIGN=LEFT><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">x
= a * (-b);
</FONT></TT><P></DIV><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
unary minus produces the negative of the value. Unary plus provides symmetry
with unary minus, although it doesn&#8217;t do much.
</FONT><a name="_Toc375545251"></a><a name="_Toc408018452"></a><P></DIV>
<A NAME="Heading106"></A><H3 ALIGN=LEFT>
Auto
increment and decrement
</H3>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Java,
like C, is full of shortcuts. Shortcuts can make code much easier to type, and
either easier or harder to read. 
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Two
of the nicer shortcuts are the increment<A NAME="Index114"></A>
and decrement<A NAME="Index115"></A>
operators (often referred to as the auto-increment<A NAME="Index116"></A>
and auto-decrement<A NAME="Index117"></A>
operators). The decrement operator is 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>--</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and means &#8220;decrease by one unit.&#8221; The increment operator is 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>++</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and means &#8220;increase by one unit.&#8221; If 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>A</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is an 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>int</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
for example, the expression 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>++A</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is equivalent to (
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>A
= A + 1
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">).
Increment and decrement operators produce the value of the variable as a result. 
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">There
are two versions of each type of operator, often called the prefix and postfix
versions. Pre-increment means the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>++
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">operator
appears before the variable or expression, and post-increment means the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>++</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
operator appears after the variable or expression. Similarly, pre-decrement
means the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>--
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">operator
appears before the variable or expression, and post-decrement means the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>--</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
operator appears after the variable or expression. For pre-increment and
pre-decrement, (i.e., 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>++A</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
or 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>--A</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">),
the operation is performed and the value is produced. For post-increment and
post-decrement (i.e. 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>A++
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">or
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>A--</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">),
the value is produced, then the operation is performed. As an example:
</FONT><P></DIV>

<font color="#990000"><PRE><font color="#009900">//: 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("i : " + i);
    prt("++i : " + ++i); <font color="#009900">// Pre-increment</font>
    prt("i++ : " + i++); <font color="#009900">// Post-increment</font>
    prt("i : " + i);
    prt("--i : " + --i); <font color="#009900">// Pre-decrement</font>
    prt("i-- : " + i--); <font color="#009900">// Post-decrement</font>
    prt("i : " + i);
  }
  <font color="#0000ff">static</font> <font color="#0000ff">void</font> prt(String s) {
    System.out.println(s);
  }
} <font color="#009900">///:~ </PRE></font></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
output for this program is:
</FONT><P></DIV>

<font color="#990000"><PRE>i : 1
++i : 2
i++ : 2
i : 3
--i : 2
i-- : 2
i : 1 </PRE></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">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="Index118"></A></FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
increment operator is one explanation for the name C++, implying &#8220;one
step beyond C.&#8221; In an early Java speech, <A NAME="Index119"></A>Bill
Joy (one of the creators), said that &#8220;Java=C++--&#8220; (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 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>that
</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">much
easier than <A NAME="Index120"></A>C++.
</FONT><a name="_Toc375545252"></a><a name="_Toc408018453"></a><P></DIV>
<A NAME="Heading107"></A><H3 ALIGN=LEFT>
Relational
operators
<P><A NAME="Index121"></A><A NAME="Index122"></A></H3>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Relational
operators generate a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>boolean</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
result. They evaluate the relationship between the values of the operands. A
relational expression produces 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>true</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
if the relationship is true, and 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>false</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
if the relationship is untrue. The relational operators are less than (&lt;)<A NAME="Index123"></A>,
greater than (&gt;)<A NAME="Index124"></A>,
less than or equal to (&lt;=)<A NAME="Index125"></A>,
greater than or equal to (&gt;=)<A NAME="Index126"></A>,
equivalent (==)<A NAME="Index127"></A>
and not equivalent (!=)<A NAME="Index128"></A>.<A NAME="Index129"></A><A NAME="Index130"></A><A NAME="Index131"></A><A NAME="Index132"></A><A NAME="Index133"></A><A NAME="Index134"></A>
Equivalence and nonequivalence works with all built-in data types, but the
other comparisons won&#8217;t work with type 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>boolean</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.</FONT><P></DIV>
<A NAME="Heading108"></A><H4 ALIGN=LEFT>
Testing
object equivalence
<P><A NAME="Index135"></A><A NAME="Index136"></A></H4>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
relational operators 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>==</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>!=</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
also work with all objects, but their meaning often confuses the first-time
Java programmer. Here&#8217;s an example:
</FONT><P></DIV>

<font color="#990000"><PRE><font color="#009900">//: 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">///:~ </PRE></font></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
expression 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>System.out.println(n1
== n2)
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
will print out the result of the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>boolean</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
comparison within it. Surely the output should be 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>true</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and then 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>false</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">since
both 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Integer</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
objects are the same. But while the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>contents</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
of the objects are the same, the <A NAME="Index137"></A><A NAME="Index138"></A>handles
are not the same and the operators 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>==</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>!=
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">compare
object handles. So the output is actually 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>false</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and then 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>true</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
Naturally, this surprises people at first.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">What
if you want to compare the actual contents of an object for equivalence? You
must use the special method <A NAME="Index139"></A><A NAME="Index140"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>equals(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
that exists for all objects (not <A NAME="Index141"></A>primitives,
which work fine with 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>==</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>!=</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">).
Here&#8217;s how it&#8217;s used:
</FONT><P></DIV>

<font color="#990000"><PRE><font color="#009900">//: 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));

⌨️ 快捷键说明

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