📄 tij305.htm
字号:
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);
monitor.expect(<font color=#0000ff>new</font> String[] {
<font color=#004488>"1: x.c: a"</font>,
<font color=#004488>"2: x.c: z"</font>
});
}
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE><p><br></p>
<p>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 <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap03_511" title="Send BackTalk Comment">Feedback</a></font><br></p>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE>y.c = 'z';</PRE></FONT></BLOCKQUOTE><p><br></p>
<p>is actually changing the object outside of <b>f( )</b>. The output in the <b>expect( ) </b>statement shows this. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap03_512" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>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. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap03_513" title="Send BackTalk Comment">Feedback</a></font><br></p>
<h3>
<a name="_Toc375545250"></a><a name="_Toc24775574"></a><a name="Heading1824"></a>Mathematical
operators</h3>
<p><a name="Index157"></a>The basic mathematical operators are the same as the ones available in most programming languages: addition (<a name="Index158"></a><b>+</b>), subtraction (<a name="Index159"></a><b>-</b>), division (<a name="Index160"></a><b>/</b>), multiplication (<a name="Index161"></a><b>*</b>) and modulus (<a name="Index162"></a><b>%</b>, which produces the remainder from integer division). Integer division truncates, rather than rounds, the result. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap03_514" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>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 size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap03_515" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>This example shows the use of the mathematical operators:<br></p>
<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> com.bruceeckel.simpletest.*;
<font color=#0000ff>import</font> java.util.*;
<font color=#0000ff>public</font> <font color=#0000ff>class</font> MathOps {
<font color=#0000ff>static</font> Test monitor = <font color=#0000ff>new</font> Test();
<font color=#009900>// Shorthand to print a string and an int:</font>
<font color=#0000ff>static</font> <font color=#0000ff>void</font> printInt(String s, <font color=#0000ff>int</font> i) {
System.out.println(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> printFloat(String s, <font color=#0000ff>float</font> f) {
System.out.println(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>// Choose value from 1 to 100:</font>
j = rand.nextInt(100) + 1;
k = rand.nextInt(100) + 1;
printInt(<font color=#004488>"j"</font>, j); printInt(<font color=#004488>"k"</font>, k);
i = j + k; printInt(<font color=#004488>"j + k"</font>, i);
i = j - k; printInt(<font color=#004488>"j - k"</font>, i);
i = k / j; printInt(<font color=#004488>"k </font><font color=#004488>/ j"</font>, i);
i = k * j; printInt(<font color=#004488>"k * j"</font>, i);
i = k % j; printInt(<font color=#004488>"k % j"</font>, i);
j %= k; printInt(<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();
printFloat(<font color=#004488>"v"</font>, v); printFloat(<font color=#004488>"w"</font>, w);
u = v + w; printFloat(<font color=#004488>"v + w"</font>, u);
u = v - w; printFloat(<font color=#004488>"v - w"</font>, u);
u = v * w; printFloat(<font color=#004488>"v * w"</font>, u);
u = v / w; printFloat(<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; printFloat(<font color=#004488>"u += v"</font>, u);
u -= v; printFloat(<font color=#004488>"u -= v"</font>, u);
u *= v; printFloat(<font color=#004488>"u *= v"</font>, u);
u /= v; printFloat(<font color=#004488>"u </font><font color=#004488>/= v"</font>, u);
monitor.expect(<font color=#0000ff>new</font> String[] {
<font color=#004488>"%% j = -?\\d+"</font>,
<font color=#004488>"%% k = -?\\d+"</font>,
<font color=#004488>"%% j \\+ k = -?\\d+"</font>,
<font color=#004488>"%% j - k = -?\\d+"</font>,
<font color=#004488>"%% k </font><font color=#004488>/ j = -?\\d+"</font>,
<font color=#004488>"%% k \\* j = -?\\d+"</font>,
<font color=#004488>"%% k % j = -?\\d+"</font>,
<font color=#004488>"%% j %= k = -?\\d+"</font>,
<font color=#004488>"%% v = -?\\d+\\.\\d+(E-?\\d)?"</font>,
<font color=#004488>"%% w = -?\\d+\\.\\d+(E-?\\d)?"</font>,
<font color=#004488>"%% v \\+ w = -?\\d+\\.\\d+(E-?\\d)??"</font>,
<font color=#004488>"%% v - w = -?\\d+\\.\\d+(E-?\\d)??"</font>,
<font color=#004488>"%% v \\* w = -?\\d+\\.\\d+(E-?\\d)??"</font>,
<font color=#004488>"%% v </font><font color=#004488>/ w = -?\\d+\\.\\d+(E-?\\d)??"</font>,
<font color=#004488>"%% u \\+= v = -?\\d+\\.\\d+(E-?\\d)??"</font>,
<font color=#004488>"%% u -= v = -?\\d+\\.\\d+(E-?\\d)??"</font>,
<font color=#004488>"%% u \\*= v = -?\\d+\\.\\d+(E-?\\d)??"</font>,
<font color=#004488>"%% u </font><font color=#004488>/= v = -?\\d+\\.\\d+(E-?\\d)??"</font>
});
}
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE><p><br></p>
<p>The first thing you will see are some shorthand methods for printing: the <b>printInt( )</b> prints a <b>String</b> followed by an <b>int</b> and the <b>printFloat( )</b> prints a <b>String</b> followed by a <b>float</b>. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap03_516" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>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 the methods: <b>nextInt( )</b> and<b> nextFloat( )</b> (you can also call <b>nextLong( ) </b>or<b> nextDouble( )</b>). <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap03_517" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>The modulus operator, when used with the result of the random number generator, limits the result to an upper bound of the operand minus 1 (99 in this case). <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap03_518" title="Send BackTalk Comment">Feedback</a></font><br></p>
<h4>
<a name="Heading1900"></a>Regular expressions</h4>
<p>Since random numbers are used to generate the output for this program, the <b>expect( )</b> statement can’t just show literal output as it did before, since the output will vary from one run to the next. To solve this problem, <i>regular expressions</i>, a new feature introduced in Java JDK 1.4 (but an old feature in languages like Perl and Python) will be used inside the <b>expect( )</b> statement. Although coverage of this intensely powerful tool doesn’t occur until Chapter 12, to understand these statements you’ll need an introduction to regular expressions. Here, you’ll learn just enough to read the <b>expect( )</b> statements, but if you want a full description, look up <b>java.util.regex.Pattern</b> in the downloadable JDK documentation. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]A0450" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>A regular expression is a way to describe strings in general terms, so that you can say: “If a string has these things in it, then it matches what I’m looking for.” For example, to say that a number might or might not be preceded by a minus sign, you put in the minus sign followed by a question mark, like this: <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]A0451" title="Send BackTalk Comment">Feedback</a></font><br></p>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE>-?</PRE></FONT></BLOCKQUOTE><p><br></p>
<p>To describe an integer, you say that it’s one or more digits. In regular expressions, a digit is ‘<b>\d</b>’, but in a Java <b>String</b> you have to “escape” the backslash by putting in a second backslash: ‘<b>\\d</b>’. To indicate “one or more of the preceding expression” in regular expressions, you use the ‘<b>+</b>’. So to say “possibly a minus sign, followed by one or more digits,” you write: <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]A0452" title="Send BackTalk Comment">Feedback</a></font><br></p>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE>-?\\d+</PRE></FONT></BLOCKQUOTE><p><br></p>
<p>Which you can see in the first lines of the <b>expect( )</b> statement in the preceding code.<br></p>
<p>One thing that is <i>not</i> part of the regular expression syntax is the ‘<b>%%</b>’ (note the space included for readability) at the beginning of the lines in the <b>expect( )</b> statement. This is a flag used by <b>simpletest</b> to indicate that the rest of the line is a regular expression. So you won’t see it in normal regular expressions, only in <b>simpletest expect( )</b> statements. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]A0453" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>Any other characters that are not special characters to regular expression searches are treated as exact matches. So in the first line:<br></p>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE>%% j = -?\\d+</PRE></FONT></BLOCKQUOTE><p><br></p>
<p>The ‘j = ’ is matched exactly. However, in the third line, the ‘+’ in ‘j + k’ must be escaped because it is a special regular expression character, as is ‘*’. The rest of the lines should be understandable from this introduction. Later in the book, when additional features of regular expressions are used inside <b>expect( )</b> statements, they will be explained. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]A0454" title="Send BackTalk Comment">Feedback</a></font><br></p>
<h4>
<a name="Heading1914"></a>Unary minus and plus operators<br></h4>
<p><a name="Index163"></a><a name="Index164"></a>The unary minus (-)and unary plus (+) 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 size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap03_519" title="Send BackTalk Comment">Feedback</a></font><br></p>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE>x = -a;</PRE></FONT></BLOCKQUOTE><p><br></p>
<p>has an obvious meaning. The compiler is able to figure out: <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap03_520" title="Send BackTalk Comment">Feedback</a></font><br></p>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE>x = a * -b;</PRE></FONT></BLOCKQUOTE><p><br></p>
<p>but the reader might get confused, so it is clearer to say: <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap03_521" title="Send BackTalk Comment">Feedback</a></font><br></p>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE>x = a * (-b);</PRE></FONT></BLOCKQUOTE><p><br></p>
<p>Unary minus inverts the sign on the data. Unary plus provides symmetry with unary minus, although it doesn’t have any effect. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap03_522" title="Send BackTalk Comment">Feedback</a></font><br></p>
<h3>
<a name="_Toc375545251"></a><a name="_Toc24775575"></a><a name="Heading1925"></a>Auto
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -