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

📄 tij305.htm

📁 这也是我们java老师给我们的thinking in java的一些资料
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html lang="en">
<!--
This document was converted from RTF source: 
By r2net 5.8 r2netcmd Windows 
See http://www.logictran.com
-->
<head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Thinking in Java, 3rd ed. Revision 4.0: 3: Controlling Program Flow</title>
<link rel="stylesheet" href="stylesheet.css" type="text/css"></head>

<body >
   <CENTER>     <a href="http://www.MindView.net">     <img src="mindview.gif" alt="MindView Inc." BORDER = "0"></a>     <Font FACE="Verdana, Tahoma, Arial, Helvetica, Sans">     <h2>Thinking in Java, 3<sup>rd</sup> ed. Revision 4.0</h2>     <FONT size = "-1"><br>     [ <a href="README.txt">Viewing Hints</a> ]     [ <a href="http://www.mindview.net/Books/TIJ/">Book Home Page</a> ]     [ <a href="http://www.mindview.net/Etc/MailingList.html">Free Newsletter</a> ] <br>     [ <a href="http://www.mindview.net/Seminars">Seminars</a> ]     [ <a href="http://www.mindview.net/CDs">Seminars on CD ROM</a> ]     [ <a href="http://www.mindview.net/Services">Consulting</a> ] <br><br>     </FONT></FONT>   </CENTER> 
<font face="Georgia"><div align="CENTER"><a href="TIJ304.htm" target="RightFrame"><img src="./prev.gif" alt="Previous " border="0"></a>
<a href="TIJ306.htm" target="RightFrame"><img src="./next.gif" alt="Next " border="0"></a>

<a href="TIJ3_t.htm"><img src="./first.gif" alt="Title Page " border="0"></a>
<a href="TIJ3_i.htm"><img src="./index.gif" alt="Index " border="0"></a>
<a href="TIJ3_c.htm"><img src="./contents.gif" alt="Contents " border="0"></a>
</div>
<hr>

<h1>
<a name="_Toc24272642"></a><a name="_Toc24775570"></a><a name="Heading1718"></a>3:
Controlling Program Flow</h1>
<p class="Intro">Like a sentient creature, a program must manipulate its world and make choices during execution. <br></p>
<p>In Java you manipulate data using operators, and you make choices with execution control statements. Java was inherited from C++, so most of these statements and operators will be familiar to C and C++ programmers. Java has also added some improvements and simplifications. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap03_497" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>If you find yourself floundering a bit in this chapter, make sure you go through the multimedia CD ROM bound into this book: <i>Foundations for Java</i>. It contains audio lectures, slides, exercises, and solutions specifically designed to bring you up to speed with the fundamentals necessary to learn Java. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap03_498" title="Send BackTalk Comment">Feedback</a></font><br></p>
<h2>
<a name="_Toc375545247"></a><a name="_Toc24775571"></a><a name="Heading1722"></a>Using
Java operators<br></h2>
<p><a name="Index144"></a><a name="Index145"></a>An operator takes one or more arguments and produces a new value. The arguments are in a different form than ordinary method calls, but the effect is the same. Addition (<b>+</b>), subtraction and unary minus (<b>-</b>), multiplication (<b>*</b>), division (<b>/</b>), and assignment (<b>=</b>) all work much the same in any programming language. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap03_499" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>All operators produce a value from their operands. In addition, an operator can change the value of an operand. This is called a <i>side effect</i>. The most common use for operators that modify their operands is to generate the side effect, but you should keep in mind that the value produced is available for your use, just as in operators without side effects. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap03_500" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p><a name="Index146"></a>Almost all operators work only with primitives. The exceptions are <b>&#145;=</b>&#146;, &#145;<b>==</b>&#146; and &#145;<b>!=</b>&#146;, which work with all objects (and are a point of confusion for objects). In addition, the <b>String </b>class supports &#145;<b>+</b>&#146; and &#145;<b>+=</b>&#146;. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap03_501" title="Send BackTalk Comment">Feedback</a></font><br></p>
<h3>
<a name="_Toc375545248"></a><a name="_Toc24775572"></a><a name="Heading1726"></a>Precedence<br></h3>
<p><a name="Index147"></a>Operator precedence defines how an expression evaluates when several operators are present. Java has specific rules that determine the order of evaluation. The easiest one to remember is that multiplication and division happen before addition and subtraction. Programmers often forget the other precedence rules, so you should use parentheses to make the order of evaluation explicit. For example: <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap03_502" title="Send BackTalk Comment">Feedback</a></font><br></p>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>a = x + y - 2/2 + z;</PRE></FONT></BLOCKQUOTE><p><br></p>
<p>has a very different meaning from the same statement with a particular grouping of parentheses: <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap03_503" title="Send BackTalk Comment">Feedback</a></font><br></p>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>a = x + (y - 2)/(2 + z);</PRE></FONT></BLOCKQUOTE><p><br></p>
<h3>
<a name="_Toc375545249"></a><a name="_Toc24775573"></a><a name="Heading1733"></a>Assignment</h3>
<p><a name="Index148"></a>Assignment is performed with the operator =. It means &#147;take the value of the right-hand side (often called the <i>rvalue</i><a name="Index149"></a>) and copy it into the left-hand side (often called the <i>lvalue</i><a name="Index150"></a>).&#148; An rvalue is any constant, variable, or expression that can produce a value, but an lvalue must be a distinct, named variable. (That is, there must be a physical space to store the value.) For instance, you can assign a constant value to a variable:<br></p>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>a = 4;</PRE></FONT></BLOCKQUOTE><p><br></p>
<p>but you cannot assign anything to constant value&#151;it cannot be an lvalue. (You can&#146;t say <b>4 = a;</b>.) <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap03_504" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>Assignment of primitives is quite straightforward. Since the primitive holds the actual value and not a reference to an object, when you assign primitives, you copy the contents from one place to another. For example, if you say <b>a = b</b> for primitives, then the contents of <b>b</b> are copied into <b>a</b>. If you then go on to modify <b>a</b>, <b>b</b> is naturally unaffected by this modification. As a programmer, this is what you&#146;ve come to expect for most situations. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap03_505" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>When you assign objects, however, things change. Whenever you manipulate an object, what you&#146;re manipulating is the reference, so when you assign &#147;from one object to another,&#148; you&#146;re actually copying a reference from one place to another. This means that if you say <a name="Index151"></a><a name="Index152"></a><a name="Index153"></a><b>c = d</b> for objects, you end up with both <b>c</b> and <b>d</b> pointing to the object that, originally, only <b>d</b> pointed to. Here&#146;s an example that demonstrates this behavior: <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap03_506" title="Send BackTalk Comment">Feedback</a></font><br></p>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c03:Assignment.java</font>
<font color=#009900>// Assignment with objects is a bit tricky.</font>
<font color=#0000ff>import</font> com.bruceeckel.simpletest.*;

<font color=#0000ff>class</font> Number {
  <font color=#0000ff>int</font> i;
}

<font color=#0000ff>public</font> <font color=#0000ff>class</font> Assignment {
  <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) {
    Number n1 = <font color=#0000ff>new</font> Number();
    Number n2 = <font color=#0000ff>new</font> Number();
    n1.i = 9;
    n2.i = 47;
    System.out.println(<font color=#004488>"1: n1.i: "</font> + n1.i +
      <font color=#004488>", n2.i: "</font> + n2.i);
    n1 = n2;
    System.out.println(<font color=#004488>"2: n1.i: "</font> + n1.i +
      <font color=#004488>", n2.i: "</font> + n2.i);
    n1.i = 27;
    System.out.println(<font color=#004488>"3: n1.i: "</font> + n1.i +
      <font color=#004488>", n2.i: "</font> + n2.i);
    monitor.expect(<font color=#0000ff>new</font> String[] {
      <font color=#004488>"1: n1.i: 9, n2.i: 47"</font>,
      <font color=#004488>"2: n1.i: 47, n2.i: 47"</font>,
      <font color=#004488>"3: n1.i: 27, n2.i: 27"</font>
    });
  }
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE><p><br></p>
<p>First, notice that something new has been added. The line:<br></p>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#0000ff>import</font> com.bruceeckel.simpletest.*;</PRE></FONT></BLOCKQUOTE><p><br></p>
<p>imports the &#147;<b>simpletest</b>&#148; library that has been created to test the code in this book, and is explained in Chapter 15. At the beginning of the <b>Assignment</b> class, you see the line:<br></p>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>  <font color=#0000ff>static</font> Test monitor = <font color=#0000ff>new</font> Test();</PRE></FONT></BLOCKQUOTE><p><br></p>
<p>This creates an instance of the <b>simpletest</b> class <b>Test</b>, called <b>monitor</b>. Finally, at the end of <b>main(&#160;)</b>, you see the statement:<br></p>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>    monitor.expect(<font color=#0000ff>new</font> String[] {
      <font color=#004488>"1: n1.i: 9, n2.i: 47"</font>,
      <font color=#004488>"2: n1.i: 47, n2.i: 47"</font>,
      <font color=#004488>"3: n1.i: 27, n2.i: 27"</font>
    });</PRE></FONT></BLOCKQUOTE><p><br></p>
<p>This is the expected output of the program, expressed as an array of <b>String</b> objects. When the program is run, it not only prints out the output, but it compares it to this array to verify that the array is correct. Thus, when you see a program in this book that uses <b>simpletest</b>, you will also see an <b>expect(&#160;)</b> call that will show you what the output of the program is. This way, you see validated output from the program.<br></p>
<p>The <b>Number</b> class is simple, and two instances of it (<b>n1</b> and <b>n2</b>) are created within <b>main(&#160;)</b>. The <b>i</b> value within each <b>Number</b> is given a different value, and then <b>n2</b> is assigned to <b>n1</b>, and <b>n1</b> is changed. In many programming languages you would expect <b>n1</b> and <b>n2</b> to be independent at all times, but because you&#146;ve assigned a reference, you&#146;ll see the output in the <b>expect(&#160;)</b> statement. Changing the <b>n1</b> object appears to change the <b>n2</b> object as well! This is because both <b>n1</b> and <b>n2</b> contain the same reference, which is pointing to the same object. (The original reference that was in <b>n1</b>, that pointed to the object holding a value of 9, was overwritten during the assignment and effectively lost; its object will be cleaned up by the garbage collector.) <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap03_508" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>This phenomenon is often called <a name="Index154"></a><a name="Index155"></a><i>aliasing</i>, and it&#146;s a fundamental way that Java works with objects. But what if you don&#146;t want aliasing to occur in this case? You could forego the assignment and say: <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap03_509" title="Send BackTalk Comment">Feedback</a></font><br></p>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>n1.i = n2.i;</PRE></FONT></BLOCKQUOTE><p><br></p>
<p>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&#146;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. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap03_510" title="Send BackTalk Comment">Feedback</a></font><br></p>
<h4>
<a name="Heading1790"></a>Aliasing during method calls<br></h4>
<p><a name="Index156"></a>Aliasing will also occur when you pass an object into a method:<br></p>

<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>import</font> com.bruceeckel.simpletest.*;

<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> Test monitor = <font color=#0000ff>new</font> Test();
  <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';

⌨️ 快捷键说明

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