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

📄 chap03.htm

📁 Thinking in Java, 2nd edition
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<!--
This document was converted from RTF source: 
By rtftohtml 4.19
See http://www.sunpack.com/RTF
Filename:TIJ2.rtf
Application Directory:C:\TOOLS\RTF2HTML\
Subject:
Author:Bruce Eckel
Operator:Bruce Eckel
Document Comments:
Version Comments:
Comments:
Keywords:
Translation Date:05/21/2001
Translation Time:10:39:05
Translation Platform:Win32
Number of Output files:23
This File:Chap03.htm
SplitDepth=1
SkipNavPanel=1
SkipLeadingToc=1
SkipTrailingToc=1
GenContents=1
GenFrames=1
GenIndex=1
-->
<HEAD lang="en"><META http-equiv="Content-Type" content="text/html">
<TITLE>3: Controlling Program Flow</TITLE>
</HEAD>

<BODY  BGCOLOR="#FFFFFF"><DIV ALIGN="CENTER">
  <a href="http://www.MindView.net">
  <img src="mindview.gif" alt="MindView Inc." BORDER = "0"></a>
  <CENTER>
    <FONT FACE="Verdana" size = "-1">
    [ <a href="README.txt">Viewing Hints</a> ]
    [ <a href="RevHist.htm">Revision History</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> ]
    </FONT>
  <H2><FONT FACE="Verdana">
  Thinking in Java, 2nd edition, Revision 12</FONT></H2>
  <H3><FONT FACE="Verdana">&copy;2000 by Bruce Eckel</FONT></H3>
  
    <FONT FACE="Verdana" size = "-1">
     [ <a href="Chap02.htm">Previous Chapter</a> ] 
    [ <a href="SimpCont.htm">Short TOC</a> ] 
    [ <a href="Contents.htm">Table of Contents</a> ] 
    [ <a href="DocIdx.htm">Index</a> ]
     [ <a href="Chap04.htm">Next Chapter</a> ] 
    </FONT>
    
  </CENTER>
  </P></DIV><A NAME="_Toc477690723"></A><A NAME="_Toc481064540"></A><A NAME="Heading125"></A><FONT FACE = "Verdana"><H1 ALIGN="LEFT">
3: Controlling Program Flow</H1></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia" SIZE=4><backtalk:display ID=TIJ3_CHAPTER3_I0>
Like a
sentient creature, a program must manipulate its world and make choices during
execution. </FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">In Java you manipulate objects and 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. 
</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER3_I0' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER3_I1>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">If you find yourself floundering a bit in
this chapter, make sure you go through the multimedia CD ROM bound into this
book: <I>Thinking in C: Foundations for Java and C++</I>. It contains audio
lectures, slides, exercises, and solutions specifically designed to bring you up
to speed with the C syntax necessary to learn Java.

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER3_I1' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER3_I2>
</FONT><A NAME="_Toc375545247"></A><A NAME="_Toc481064541"></A><BR></P></DIV>
<A NAME="Heading126"></A><FONT FACE = "Verdana"><H2 ALIGN="LEFT">
Using Java operators<BR><A NAME="Index198"></A><A NAME="Index199"></A></H2></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">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. You should be reasonably comfortable
with the general concept of operators from your previous programming experience.
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.

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER3_I2' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER3_I3>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">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<A NAME="Index200"></A></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.

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER3_I3' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER3_I4>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Almost all operators work only with
primitives. The exceptions are <B>&#8216;=</B>&#8217;, &#8216;<B>==</B>&#8217;
and &#8216;<B>!=</B>&#8217;, which work with all objects (and are a point of
confusion for objects). In addition, the <B>String </B>class supports
&#8216;<B>+</B>&#8217; and &#8216;<B>+=</B>&#8217;.

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER3_I4' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER3_I5>
</FONT><A NAME="_Toc375545248"></A><A NAME="_Toc481064542"></A><BR></P></DIV>
<A NAME="Heading127"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Precedence<BR><A NAME="Index201"></A></H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">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:

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER3_I5' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER3_I6>
</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>A a = X x + Y y - 2/2 + Zz;</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">has a very different meaning from the
same statement with a particular grouping of parentheses:

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER3_I6' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER3_I7>
</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>A a = X x + (Y y - 2)/(2 + Zz);</PRE></FONT></BLOCKQUOTE><DIV ALIGN="LEFT"><P><A NAME="_Toc375545249"></A><A NAME="_Toc481064543"></A><BR></P></DIV>
<A NAME="Heading128"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Assignment<A NAME="Index202"></A></H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Assignment is performed with the operator
=. It means &#8220;take the value of the right-hand side (often called the
<I>rvalue<A NAME="Index203"></A></I>) and copy it into the left-hand side (often
called the <I>lvalue<A NAME="Index204"></A></I>). 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 a
value.) For instance, you can assign a constant value to a variable (<B>A =
4;</B>), but you cannot assign anything to constant value&#8212;it cannot be an
lvalue. (You can&#8217;t say <B>4 = A;</B>.)

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER3_I7' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER3_I8>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">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&#8217;ve come to expect for most situations.

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER3_I8' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER3_I9>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">When you
<A NAME="Index205"></A><A NAME="Index206"></A><A NAME="Index207"></A><A NAME="Index208"></A>assign
objects, however, things change. Whenever you manipulate an object, what
you&#8217;re manipulating is the reference, so when you assign &#8220;from one
object to another&#8221; you&#8217;re actually copying a reference from one
place to another. This means that if you say <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. The following example will demonstrate this. 

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER3_I9' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER3_I10>
</FONT><A NAME="Running_programs"></A><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Here&#8217;s the
example:</FONT><BR></P></DIV>

<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>class</font> Number {
  <font color=#0000ff>int</font> i;
}

<font color=#0000ff>public</font> <font color=#0000ff>class</font> Assignment {
  <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);
  }
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">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&#8217;ve assigned a reference
here&#8217;s the output you&#8217;ll see:

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER3_I10' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER3_I11>
</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>1: n1.i: 9, n2.i: 47
2: n1.i: 47, n2.i: 47
3: n1.i: 27, n2.i: 27</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">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.)

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER3_I11' 

⌨️ 快捷键说明

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