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

📄 chapter03.html

📁 java 是一个很好的网络开发环境。由于它是通过解释的方法
💻 HTML
📖 第 1 页 / 共 5 页
字号:
can add surprises.</FONT><BR></P></DIV>
<A NAME="Heading102"></A><FONT FACE = "Verdana"><H4 ALIGN="LEFT">
Aliasing during method calls<BR><A NAME="Index101"></A></H4></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Aliasing will also occur when you
pass an object into a method:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: PassObject.java</font>
<font color=#009900>// Passing objects to methods can be a bit tricky</font>

<font color=#0000ff>class</font> Letter {
  <font color=#0000ff>char</font> c;
}

<font color=#0000ff>public</font> <font color=#0000ff>class</font> PassObject {
  <font color=#0000ff>static</font> <font color=#0000ff>void</font> f(Letter y) {
    y.c = 'z';
  }
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> main(String[] args) {
    Letter x = <font color=#0000ff>new</font> Letter();
    x.c = 'a';
    System.out.println(<font color=#004488>"1: x.c: "</font> + x.c);
    f(x);
    System.out.println(<font color=#004488>"2: x.c: "</font> + x.c);
  }
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">In many programming languages, the
method <B>f(&#160;)</B> would appear to be making a copy of its argument
<B>Letter y</B> inside the scope of the method. But once again a handle is being
passed so the line</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>y.c = 'z';</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">is actually changing the object
outside of <B>f(&#160;)</B>. The output shows this:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>1: x.c: a
2: x.c: z</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Aliasing and its solution is a
complex issue and, although you must wait until Chapter 12 for all the answers,
you should be aware of it at this point so you can watch for
pitfalls.</FONT><A NAME="_Toc375545250"></A><A NAME="_Toc408018451"></A><BR></P></DIV>
<A NAME="Heading103"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Mathematical operators<A NAME="Index102"></A></H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The basic mathematical operators
are the same as the ones available in most programming languages: addition
<A NAME="Index103"></A>(<B>+</B>), subtraction<A NAME="Index104"></A>
(<B>-</B>), division<A NAME="Index105"></A> (<B>/</B>),
multiplication<A NAME="Index106"></A> (<B>*</B>) and
modulus<A NAME="Index107"></A> (<B>%</B>, produces the remainder from integer
division). Integer division truncates, rather than rounds, the
result.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Java also uses a shorthand notation
to perform an operation and an assignment at the same time. This is denoted by
an operator followed by an equal sign, and is consistent with all the operators
in the language (whenever it makes sense). For example, to add 4 to the variable
<B>x</B> and assign the result<B> </B>to <B>x</B>, use: <B>x += 4;</B>.
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">This example shows the use of the
mathematical operators:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: MathOps.java</font>
<font color=#009900>// Demonstrates the mathematical operators</font>
<font color=#0000ff>import</font> java.util.*;

<font color=#0000ff>public</font> <font color=#0000ff>class</font> MathOps {
  <font color=#009900>// Create a shorthand to save typing:</font>
  <font color=#0000ff>static</font> <font color=#0000ff>void</font> prt(String s) {
    System.out.println(s);
  }
  <font color=#009900>// shorthand to print a string and an int:</font>
  <font color=#0000ff>static</font> <font color=#0000ff>void</font> pInt(String s, <font color=#0000ff>int</font> i) {
    prt(s + <font color=#004488>" = "</font> + i);
  }
  <font color=#009900>// shorthand to print a string and a float:</font>
  <font color=#0000ff>static</font> <font color=#0000ff>void</font> pFlt(String s, <font color=#0000ff>float</font> f) {
    prt(s + <font color=#004488>" = "</font> + f);
  }
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> main(String[] args) {
    <font color=#009900>// Create a random number generator,</font>
    <font color=#009900>// seeds with current time by default:</font>
    Random rand = <font color=#0000ff>new</font> Random();
    <font color=#0000ff>int</font> i, j, k;
    <font color=#009900>// '%' limits maximum value to 99:</font>
    j = rand.nextInt() % 100;
    k = rand.nextInt() % 100;
    pInt(<font color=#004488>"j"</font>,j);  pInt(<font color=#004488>"k"</font>,k);
    i = j + k; pInt(<font color=#004488>"j + k"</font>, i);
    i = j - k; pInt(<font color=#004488>"j - k"</font>, i);
    i = k / j; pInt(<font color=#004488>"k </font><font color=#004488>/ j"</font>, i);
    i = k * j; pInt(<font color=#004488>"k * j"</font>, i);
    i = k % j; pInt(<font color=#004488>"k % j"</font>, i);
    j %= k; pInt(<font color=#004488>"j %= k"</font>, j);
    <font color=#009900>// Floating-point number tests:</font>
    <font color=#0000ff>float</font> u,v,w;  <font color=#009900>// applies to doubles, too</font>
    v = rand.nextFloat();
    w = rand.nextFloat();
    pFlt(<font color=#004488>"v"</font>, v); pFlt(<font color=#004488>"w"</font>, w);
    u = v + w; pFlt(<font color=#004488>"v + w"</font>, u);
    u = v - w; pFlt(<font color=#004488>"v - w"</font>, u);
    u = v * w; pFlt(<font color=#004488>"v * w"</font>, u);
    u = v / w; pFlt(<font color=#004488>"v </font><font color=#004488>/ w"</font>, u);
    <font color=#009900>// the following also works for</font>
    <font color=#009900>// char, byte, short, int, long,</font>
    <font color=#009900>// and double:</font>
    u += v; pFlt(<font color=#004488>"u += v"</font>, u);
    u -= v; pFlt(<font color=#004488>"u -= v"</font>, u);
    u *= v; pFlt(<font color=#004488>"u *= v"</font>, u);
    u /= v; pFlt(<font color=#004488>"u </font><font color=#004488>/= v"</font>, u);
  }
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The first thing you will see are
some shorthand methods for printing: the <B>prt(&#160;)</B> method prints a
<B>String</B>, the <B>pInt(&#160;)</B> prints a <B>String</B> followed by an
<B>int</B> and the <B>pFlt(&#160;)</B> prints a <B>String</B> followed by a
<B>float</B>. Of course, they all ultimately end up using
<B>System.out.println(&#160;)</B>.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">To generate numbers, the program
first creates a <B>Random</B> object. Because no arguments are passed during
creation, Java uses the current time as a seed for the random number generator.
The program generates a number of different types of random numbers with the
<B>Random</B> object simply by calling different methods:
<B>nextInt(&#160;)</B>, <B>nextLong(&#160;)</B>,<B> nextFloat(&#160;)</B> or<B>
nextDouble(&#160;)</B>. </FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">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><BR></P></DIV>
<A NAME="Heading104"></A><FONT FACE = "Verdana"><H4 ALIGN="LEFT">
Unary minus and plus
operators<BR><A NAME="Index108"></A><A NAME="Index109"></A></H4></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">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><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>x = -a;</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">has an obvious meaning. The
compiler is able to figure out:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>x = a * -b;</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">but the reader might get confused,
so it is more clear to say:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>x = a * (-b);</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">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><BR></P></DIV>
<A NAME="Heading105"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Auto increment and decrement</H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Java, like C, is full of shortcuts.
Shortcuts can make code much easier to type, and either easier or harder to
read. </FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">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
<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. </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:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><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(<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:</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="Index118"></A></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="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 <I>that </I>much easier than

⌨️ 快捷键说明

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