📄 chap03.htm
字号:
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER3_I12>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">This phenomenon is often called
<A NAME="Index209"></A><A NAME="Index210"></A><I>aliasing</I> and it’s a
fundamental way that Java works with objects. But what if you don’t want
aliasing to occur in this case? You could forego the assignment and say:
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER3_I12'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER3_I13>
</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE>n1.i = n2.i;</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">This retains the two separate objects
instead of tossing one and tying <B>n1 </B>and <B>n2</B> to the same object, but
you’ll soon realize that manipulating the fields within objects is messy
and goes against good object-oriented design principles. This is a nontrivial
topic, so it is left for Appendix A, which is devoted to aliasing. In the
meantime, you should keep in mind that assignment for objects can add surprises.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER3_I13'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER3_I14>
</FONT><BR></P></DIV>
<A NAME="Heading129"></A><FONT FACE = "Verdana"><H4 ALIGN="LEFT">
Aliasing during method calls<BR><A NAME="Index211"></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>//: c03:PassObject.java</font>
<font color=#009900>// Passing objects to methods may not be what</font>
<font color=#009900>// you're used to.</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( )</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 reference is being passed
so the line
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER3_I14'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER3_I15>
</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( )</B>. The output shows this:
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER3_I15'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER3_I16>
</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 Appendix A for all the answers, you
should be aware of it at this point so you can watch for pitfalls.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER3_I16'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER3_I17>
</FONT><A NAME="_Toc375545250"></A><A NAME="_Toc481064544"></A><BR></P></DIV>
<A NAME="Heading130"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Mathematical operators<A NAME="Index212"></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="Index213"></A>(<B>+</B>), subtraction<A NAME="Index214"></A>
(<B>-</B>), division<A NAME="Index215"></A> (<B>/</B>),
multiplication<A NAME="Index216"></A> (<B>*</B>) and
modulus<A NAME="Index217"></A> (<B>%</B>, which produces the remainder from
integer division). Integer division truncates, rather than rounds, the result.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER3_I17'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER3_I18>
</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>.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER3_I18'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER3_I19>
</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>//: c03: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( )</B> method prints a
<B>String</B>, the <B>pInt( )</B> prints a <B>String</B> followed by an
<B>int</B> and the <B>pFlt( )</B> prints a <B>String</B> followed by a
<B>float</B>. Of course, they all ultimately end up using
<B>System.out.println( )</B>.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER3_I19'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER3_I20>
</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( )</B>, <B>nextLong( )</B>,<B> nextFloat( )</B> or<B>
nextDouble( )</B>.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER3_I20'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER3_I21>
</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).
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER3_I21'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER3_I22>
</FONT><BR></P></DIV>
<A NAME="Heading131"></A><FONT FACE = "Verdana"><H4 ALIGN="LEFT">
Unary minus and plus
operators<BR><A NAME="Index218"></A><A NAME="Index219"></A></H4></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The unary minus
(-)<A NAME="Index220"></A><A NAME="Index221"></A> and unary plus
(+)<A NAME="Index222"></A><A NAME="Index223"></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
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER3_I22'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER3_I23>
</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:
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER3_I23'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER3_I24>
</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 clearer to say:
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER3_I24'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER3_I25>
</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’t have any effect.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER3_I25'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER3_I26>
</FONT><A NAME="_Toc375545251"></A><A NAME="_Toc481064545"></A><BR></P></DIV>
<A NAME="Heading132"></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.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -