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

📄 tij0044.html

📁 学习java的经典书籍
💻 HTML
📖 第 1 页 / 共 5 页
字号:
is changed. In many programming languages you would expect 
</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">
to be independent at all times, but because you&#8217;ve assigned a handle
here&#8217;s the output you&#8217;ll see:
</FONT><P></DIV>

<font color="#990000"><PRE>1: n1.i: 9, n2.i: 47
2: n1.i: 47, n2.i: 47
3: n1.i: 27, n2.i: 27 </PRE></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Changing
the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>n1</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object appears to change the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>n2</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object as well! This is because both 
</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">
contain the same handle, which is pointing to the same object. (The original
handle that was in 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>n1</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
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><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">This
phenomenon is often called <A NAME="Index99"></A><A NAME="Index100"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>aliasing</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and it&#8217;s a fundamental way that Java works with objects. But what if you
don&#8217;t want aliasing to occur in this case? You could forego the
assignment and say:
</FONT><P></DIV><DIV ALIGN=LEFT><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">n1.i
= n2.i;
</FONT></TT><P></DIV><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">This
retains the two separate objects instead of tossing one and tying 
</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">
to the same object, but you&#8217;ll soon realize that manipulating the fields
within objects is messy and goes against good object-oriented design
principles. This is a non-trivial topic, so it is left for Chapter 12, which is
devoted to aliasing. In the meantime, you should keep in mind that assignment
for objects can add surprises.
</FONT><P></DIV>
<A NAME="Heading103"></A><H4 ALIGN=LEFT>
Aliasing
during method calls
<P><A NAME="Index101"></A></H4>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Aliasing
will also occur when you pass an object into a method:
</FONT><P></DIV>

<font color="#990000"><PRE><font color="#009900">//: PassObject.java</font>
<font color="#009900">// Passing objects to methods can be a bit tricky</font>

<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> <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';
    System.out.println("1: x.c: " + x.c);
    f(x);
    System.out.println("2: x.c: " + x.c);
  }
} <font color="#009900">///:~ </PRE></font></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">In
many programming languages, the method 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>f(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
would appear to be making a copy of its argument 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Letter
y
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
inside the scope of the method. But once again a handle is being passed so the
line
</FONT><P></DIV><DIV ALIGN=LEFT><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">y.c
= 'z';
</FONT></TT><P></DIV><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">is
actually changing the object outside of 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>f(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
The output shows this:
</FONT><P></DIV>

<font color="#990000"><PRE>1: x.c: a
2: x.c: z </PRE></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Aliasing
and its solution is a complex issue and, although you must wait until Chapter
12 for all the answers, you should be aware of it at this point so you can
watch for pitfalls.
</FONT><a name="_Toc375545250"></a><a name="_Toc408018451"></a><P></DIV>
<A NAME="Heading104"></A><H3 ALIGN=LEFT>
Mathematical
operators<A NAME="Index102"></A></H3>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
basic mathematical operators are the same as the ones available in most
programming languages: addition <A NAME="Index103"></A>(</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>+</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">),
subtraction<A NAME="Index104"></A>
(
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>-</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">),
division<A NAME="Index105"></A>
(
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>/</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">),
multiplication<A NAME="Index106"></A>
(
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>*</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">)
and modulus<A NAME="Index107"></A>
(
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>%</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
produces the remainder from integer division). Integer division truncates,
rather than rounds, the result.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">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 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>x</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and assign the result
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">to
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>x</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
use: 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>x
+= 4;
</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">This
example shows the use of the mathematical operators:
</FONT><P></DIV>

<font color="#990000"><PRE><font color="#009900">//: MathOps.java</font>
<font color="#009900">// Demonstrates the mathematical operators</font>
<font color="#0000ff">import</font> java.util.*;

<font color="#0000ff">public</font> <font color="#0000ff">class</font> MathOps {
  <font color="#009900">// Create a shorthand to save typing:</font>
  <font color="#0000ff">static</font> <font color="#0000ff">void</font> prt(String s) {
    System.out.println(s);
  }
  <font color="#009900">// shorthand to print a string and an int:</font>
  <font color="#0000ff">static</font> <font color="#0000ff">void</font> pInt(String s, <font color="#0000ff">int</font> i) {
    prt(s + " = " + i);
  }
  <font color="#009900">// shorthand to print a string and a float:</font>
  <font color="#0000ff">static</font> <font color="#0000ff">void</font> pFlt(String s, <font color="#0000ff">float</font> f) {
    prt(s + " = " + 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">// '%' limits maximum value to 99:</font>
    j = rand.nextInt() % 100;
    k = rand.nextInt() % 100;
    pInt("j",j);  pInt("k",k);
    i = j + k; pInt("j + k", i);
    i = j - k; pInt("j - k", i);
    i = k / j; pInt("k / j", i);
    i = k * j; pInt("k * j", i);
    i = k % j; pInt("k % j", i);
    j %= k; pInt("j %= k", 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();
    pFlt("v", v); pFlt("w", w);
    u = v + w; pFlt("v + w", u);
    u = v - w; pFlt("v - w", u);
    u = v * w; pFlt("v * w", u);
    u = v / w; pFlt("v / w", 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; pFlt("u += v", u);
    u -= v; pFlt("u -= v", u);
    u *= v; pFlt("u *= v", u);
    u /= v; pFlt("u /= v", u);
  }
} <font color="#009900">///:~ </PRE></font></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
first thing you will see are some shorthand methods for printing: the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>prt(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
method prints a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>String</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>pInt(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
prints a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>String</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
followed by an 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>int</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>pFlt(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
prints a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>String</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
followed by a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>float</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
Of course, they all ultimately end up using 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>System.out.println(&#160;)</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">To
generate numbers, the program first creates a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Random</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
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 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Random</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object simply by calling different methods: 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>nextInt(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>nextLong(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>
nextFloat(&#160;)
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
or
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>
nextDouble(&#160;)

⌨️ 快捷键说明

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