📄 tij305.htm
字号:
increment and decrement</h3>
<p>Java, like C, is full of shortcuts. Shortcuts can make code much easier to type, and either easier or harder to read. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap03_523" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>Two of the nicer shortcuts are the increment and decrement operators (often referred to as the auto-increment and auto-decrement operators). The decrement operator is <a name="Index168"></a><a name="Index169"></a><a name="Index170"></a><a name="Index171"></a><b>--</b> and means “decrease by one unit.” The increment operator is <b>++</b> and means “increase by one unit.” 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 not only modify the variable, but also produce the value of the variable as a result. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap03_524" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>There are two versions of each type of operator, often called the <i>prefix</i> and <i>postfix</i> versions. <i>Pre-increment</i> means the <b>++ </b>operator appears before the variable or expression, and <i>post-increment</i> means the <b>++</b> operator appears after the variable or expression. Similarly, <i>pre-decrement</i> means the <b>-- </b>operator appears before the variable or expression, and <i>post-decrement</i> 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 size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap03_525" title="Send BackTalk Comment">Feedback</a></font><br></p>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c03:AutoInc.java</font>
<font color=#009900>// Demonstrates the ++ and -- operators.</font>
<font color=#0000ff>import</font> com.bruceeckel.simpletest.*;
<font color=#0000ff>public</font> <font color=#0000ff>class</font> AutoInc {
<font color=#0000ff>static</font> Test monitor = <font color=#0000ff>new</font> Test();
<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;
System.out.println(<font color=#004488>"i : "</font> + i);
System.out.println(<font color=#004488>"++i : "</font> + ++i); <font color=#009900>// Pre-increment</font>
System.out.println(<font color=#004488>"i++ : "</font> + i++); <font color=#009900>// Post-increment</font>
System.out.println(<font color=#004488>"i : "</font> + i);
System.out.println(<font color=#004488>"--i : "</font> + --i); <font color=#009900>// Pre-decrement</font>
System.out.println(<font color=#004488>"i-- : "</font> + i--); <font color=#009900>// Post-decrement</font>
System.out.println(<font color=#004488>"i : "</font> + i);
monitor.expect(<font color=#0000ff>new</font> String[] {
<font color=#004488>"i : 1"</font>,
<font color=#004488>"++i : 2"</font>,
<font color=#004488>"i++ : 2"</font>,
<font color=#004488>"i : 3"</font>,
<font color=#004488>"--i : 2"</font>,
<font color=#004488>"i-- : 2"</font>,
<font color=#004488>"i : 1"</font>
});
}
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE><p><br></p>
<p>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.) <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap03_527" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p><a name="Index172"></a>The increment operator is one explanation for the name C++, implying “one step beyond C.” In an early Java speech, Bill Joy (one of the Java creators), said that “Java=C++--” (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’ll see that many parts are simpler, and yet Java isn’t <a name="Index173"></a><i>that </i>much easier than C++. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap03_528" title="Send BackTalk Comment">Feedback</a></font><br></p>
<h3>
<a name="Index174"></a><a name="_Toc375545252"></a><a name="_Toc24775576"></a><a name="Heading1958"></a>Relational
operators<br></h3>
<p><a name="Index175"></a><a name="Index176"></a>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 (<)<a name="Index177"></a>, greater than (>), less than or equal to (<=), greater than or equal to (>=), equivalent (==) and not equivalent (!=). Equivalence and nonequivalence work with all built-in data types, but the other comparisons won’t work with type <a name="Index178"></a><a name="Index179"></a><a name="Index180"></a><a name="Index181"></a><a name="Index182"></a><a name="Index183"></a><a name="Index184"></a><a name="Index185"></a><a name="Index186"></a><a name="Index187"></a><a name="Index188"></a><b>boolean</b><a name="Index189"></a>. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap03_529" title="Send BackTalk Comment">Feedback</a></font><br></p>
<h4>
<a name="Heading1960"></a>Testing object equivalence<br></h4>
<p><a name="Index190"></a><a name="Index191"></a>The relational operators <b>==</b> and <b>!=</b> also work with all objects, but their meaning often confuses the first-time Java programmer. Here’s an example: <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap03_530" title="Send BackTalk Comment">Feedback</a></font><br></p>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c03:Equivalence.java</font>
<font color=#0000ff>import</font> com.bruceeckel.simpletest.*;
<font color=#0000ff>public</font> <font color=#0000ff>class</font> Equivalence {
<font color=#0000ff>static</font> Test monitor = <font color=#0000ff>new</font> Test();
<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);
monitor.expect(<font color=#0000ff>new</font> String[] {
<font color=#004488>"false"</font>,
<font color=#004488>"true"</font>
});
}
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE><p><br></p>
<p>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 references are not the same and the operators <a name="Index192"></a><a name="Index193"></a><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. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap03_531" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>What if you want to compare the actual contents of an object for equivalence? You must use the special method <a name="Index194"></a><a name="Index195"></a><b>equals( )</b> that exists for all objects (not primitives, which work fine with <a name="Index196"></a><b>==</b> and <b>!=</b>). Here’s how it’s used: <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap03_532" title="Send BackTalk Comment">Feedback</a></font><br></p>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c03:EqualsMethod.java</font>
<font color=#0000ff>import</font> com.bruceeckel.simpletest.*;
<font color=#0000ff>public</font> <font color=#0000ff>class</font> EqualsMethod {
<font color=#0000ff>static</font> Test monitor = <font color=#0000ff>new</font> Test();
<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));
monitor.expect(<font color=#0000ff>new</font> String[] {
<font color=#004488>"true"</font>
});
}
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE><p><br></p>
<p>The result will be <b>true</b>, as you would expect. Ah, but it’s not as simple as that. If you create your own class, like this: <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap03_533" title="Send BackTalk Comment">Feedback</a></font><br></p>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c03:EqualsMethod2.java</font>
<font color=#0000ff>import</font> com.bruceeckel.simpletest.*;
<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>static</font> Test monitor = <font color=#0000ff>new</font> Test();
<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));
monitor.expect(<font color=#0000ff>new</font> String[] {
<font color=#004488>"false"</font>
});
}
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE><p><br></p>
<p>you’re back to square one: the result is <b>false</b>. This is because the default behavior of <b>equals( )</b> is to compare references. So unless you <i>override</i> <b>equals( )</b> in your new class you won’t get the desired behavior. Unfortunately, you won’t learn about overriding until Chapter 7 and about the proper way to define <b>equals( )</b> until Chapter 11, but being aware of the way <b>equals( )</b> behaves might save you some grief in the meantime. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap03_534" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>Most of the Java library classes implement <b>equals( )</b> so that it compares the contents of objects instead of their references. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap03_535" title="Send BackTalk Comment">Feedback</a></font><br></p>
<h3>
<a name="_Toc375545253"></a><a name="_Toc24775577"></a><a name="Heading2019"></a>Logical
operators</h3>
<p><a name="Index197"></a><a name="Index198"></a>Each of the logical operators AND (&&), OR (||) and NOT (!) produces a <a name="Index199"></a><a name="Index200"></a><a name="Index201"></a><a name="Index202"></a><a name="Index203"></a><a name="Index204"></a><b>boolean</b> value of <b>true</b> or <b>false</b> based on the logical relationship of its arguments. This example uses the relational and logical operators: <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap03_536" title="Send BackTalk Comment">Feedback</a></font><br></p>
<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> com.bruceeckel.simpletest.*;
<font color=#0000ff>import</font> java.util.*;
<font color=#0000ff>public</font> <font color=#0000ff>class</font> Bool {
<font color=#0000ff>static</font> Test monitor = <font color=#0000ff>new</font> Test();
<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);
<font color=#0000ff>int</font> j = rand.nextInt(100);
System.out.println(<font color=#004488>"i = "</font> + i);
System.out.println(<font color=#004488>"j = "</font> + j);
System.out.println(<font color=#004488>"i > j is "</font> + (i > j));
System.out.println(<font color=#004488>"i < j is "</font> + (i < j));
System.out.println(<font color=#004488>"i >= j is "</font> + (i >= j));
System.out.println(<font color=#004488>"i <= j is "</font> + (i <= j));
System.out.println(<font color=#004488>"i == j is "</font> + (i == j));
System.out.println(<font color=#004488>"i != j is "</font> + (i != j));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -