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

📄 tij0044.html

📁 学习java的经典书籍
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<html><body>

<table width="100%"><tr>
<td>
<a href="http://www.bruceeckel.com/javabook.html">Bruce Eckel's Thinking in Java</a>
</td>
<td align="right">
<a href="tij_c.html">Contents</a> | <a href="tij0043.html">Prev</a> | <a href="tij0045.html">Next</a>
</td>
</tr></table>
<hr>

<H2 ALIGN=LEFT>
Using
Java operators
<P><A NAME="Index84"></A><A NAME="Index85"></A></H2>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">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 (
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>+</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">),
subtraction and unary minus (
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>-</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">),
multiplication (
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>*</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">),
division (
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>/</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">)
and assignment (
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>=</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">)
all work much the same in any programming language.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">All
operators produce a value from their operands. In addition, an operator can
change the value of an operand. This is called a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>side
effect<A NAME="Index86"></A></I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
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><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Almost
all operators work only with primitives. The exceptions are 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>&#8216;=</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">&#8217;,
&#8216;
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>==</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">&#8217;
and &#8216;
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>!=</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">&#8217;,
which work with all objects (and are a point of confusion for objects). In
addition, the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>String
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">class
supports &#8216;
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>+</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">&#8217;
and &#8216;
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>+=</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">&#8217;.</FONT><a name="_Toc375545248"></a><a name="_Toc408018449"></a><P></DIV>
<A NAME="Heading101"></A><H3 ALIGN=LEFT>
Precedence<P><A NAME="Index87"></A></H3>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">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><P></DIV><DIV ALIGN=LEFT><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">A
= X + Y - 2/2 + Z;
</FONT></TT><P></DIV><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">has
a very different meaning from the same statement with a particular grouping of
parentheses:
</FONT><P></DIV><DIV ALIGN=LEFT><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">A
= X + (Y - 2)/(2 + Z);
</FONT></TT><P></DIV><DIV ALIGN=LEFT><a name="_Toc375545249"></a><a name="_Toc408018450"></a><P></DIV>
<A NAME="Heading102"></A><H3 ALIGN=LEFT>
Assignment<A NAME="Index88"></A></H3>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Assignment
is performed with the operator =. It means &#8220;take the value of the
right-hand side (often called the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>rvalue<A NAME="Index89"></A></I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">)
and copy it into the left-hand side (often called the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>lvalue<A NAME="Index90"></A></I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">).
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 (
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>A
= 4;
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">),
but you cannot assign anything to constant value &#8211; it cannot be an
lvalue. (You can&#8217;t say 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>4
= A;.
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">)</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Assignment
of primitives is quite straightforward. Since the primitive holds the actual
value and not a handle to an object, when you assign primitives you copy the
contents from one place to another. For example, if you say 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>A
= B
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
for primitives, then the contents of 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>B</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is copied into 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>A</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
If you then go on to modify 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>A</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>B</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is naturally unaffected by this modification. This is what you&#8217;ve come to
expect as a programmer for most situations.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">When
you <A NAME="Index91"></A><A NAME="Index92"></A><A NAME="Index93"></A><A NAME="Index94"></A>assign
objects, however, things change. Whenever you manipulate an object, what
you&#8217;re manipulating is the handle, so when you assign &#8220;from one
object to another&#8221; you&#8217;re actually copying a handle from one place
to another. This means that if you say 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>C
= D
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
for objects, you end up with both 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>C</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>D</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
pointing to the object that, originally, only 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>D</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
pointed to. The following example will demonstrate this. 
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">As
an aside, the first thing you see is a <A NAME="Index95"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>package</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
statement for 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>package
c03
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
indicating this book&#8217;s Chapter 3. The first code listing of each chapter
will contain a package statement like this to establish the chapter number for
the remaining code listings in that chapter. In Chapter 17, you&#8217;ll see
that as a result, all the listings in chapter 3 (except those that have
different package names) will be automatically placed in a subdirectory called 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>c03</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
Chapter 4&#8217;s listings will be in 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>c04</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and so on. All this will happen via the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>CodePackager.java</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
program shown in Chapter 17, and in Chapter 5 the concept of packages will be
fully explained. What you need to recognize at this point is that, for this
book, lines of code of the form 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>package
c03
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
are used just to establish the chapter subdirectory for the listings in the
chapter.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">In
order to run the program, you must ensure that the <A NAME="Index96"></A><A NAME="Index97"></A><A NAME="Index98"></A>classpath
contains the root directory where you installed the source code for this book.
(From this directory, you&#8217;ll see the subdirectories 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>c02</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>c03</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>c04</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
etc.)
</FONT><a name="Running_programs"></a><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">For
later versions of Java (1.1.4 and on), when your 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>main(&#160;)
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">is
inside a file with a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>package
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">statement,
you must give the full package name before the program name in order to run the
program. In this case, the command line is:
</FONT><P></DIV><DIV ALIGN=LEFT><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">java
c03.Assignment
</FONT></TT><P></DIV><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Keep
this in mind any time you&#8217;re running a program that&#8217;s in a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>package</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Here&#8217;s
the example:
</FONT><P></DIV>

<font color="#990000"><PRE><font color="#009900">//: Assignment.java</font>
<font color="#009900">// Assignment with objects is a bit tricky</font>
<font color="#0000ff">package</font> c03;

<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("1: n1.i: " + n1.i +
      ", n2.i: " + n2.i);
    n1 = n2;
    System.out.println("2: n1.i: " + n1.i +
      ", n2.i: " + n2.i);
    n1.i = 27;
    System.out.println("3: n1.i: " + n1.i +
      ", n2.i: " + n2.i);
  }
} <font color="#009900">///:~ </PRE></font></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Number</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
class is simple, and two instances of it (
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>n1</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>n2</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">)
are created within 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>main(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
The 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>i</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
value within each 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Number</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is given a different value, and then 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>n2</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is assigned to 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>n1</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
and 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>n1</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">

⌨️ 快捷键说明

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