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

📄 chapter04.html

📁 java 是一个很好的网络开发环境。由于它是通过解释的方法
💻 HTML
📖 第 1 页 / 共 5 页
字号:
  <font color=#0000ff>protected</font> <font color=#0000ff>void</font> finalize() {
    <font color=#0000ff>if</font>(!gcrun) {
      gcrun = <font color=#0000ff>true</font>;
      System.out.println(
        <font color=#004488>"Beginning to finalize after "</font> +
        created + <font color=#004488>" Chairs have been created"</font>);
    }
    <font color=#0000ff>if</font>(i == 47) {
      System.out.println(
        <font color=#004488>"Finalizing Chair #47, "</font> +
        <font color=#004488>"Setting flag to stop Chair creation"</font>);
      f = <font color=#0000ff>true</font>;
    }
    finalized++;
    <font color=#0000ff>if</font>(finalized &gt;= created)
      System.out.println(
        <font color=#004488>"All "</font> + finalized + <font color=#004488>" finalized"</font>);
  }
}

<font color=#0000ff>public</font> <font color=#0000ff>class</font> Garbage {
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> main(String[] args) {
    <font color=#0000ff>if</font>(args.length == 0) {
      System.err.println(<font color=#004488>"Usage: \n"</font> +
        <font color=#004488>"java Garbage before\n  or:\n"</font> +
        <font color=#004488>"java Garbage after"</font>);
      <font color=#0000ff>return</font>;
    }
    <font color=#0000ff>while</font>(!Chair.f) {
      <font color=#0000ff>new</font> Chair();
      <font color=#0000ff>new</font> String(<font color=#004488>"To take up space"</font>);
    }
    System.out.println(
      <font color=#004488>"After all Chairs have been created:\n"</font> +
      <font color=#004488>"total created = "</font> + Chair.created +
      <font color=#004488>", total finalized = "</font> + Chair.finalized);
    <font color=#0000ff>if</font>(args[0].equals(<font color=#004488>"before"</font>)) {
      System.out.println(<font color=#004488>"gc():"</font>);
      System.gc();
      System.out.println(<font color=#004488>"runFinalization():"</font>);
      System.runFinalization();
    }
    System.out.println(<font color=#004488>"bye!"</font>);
    <font color=#0000ff>if</font>(args[0].equals(<font color=#004488>"after"</font>))
      System.runFinalizersOnExit(<font color=#0000ff>true</font>);
  }
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The above program creates many
<B>Chair</B> objects, and at some point after the garbage collector begins
running, the program stops creating <B>Chair</B>s. Since the garbage collector
can run at any time, you don&#8217;t know exactly when it will start up, so
there&#8217;s a flag called <B>gcrun</B> to indicate whether the garbage
collector has started running yet. A second flag <B>f</B> is a way for
<B>Chair</B> to tell the <B>main(&#160;)</B> loop that it should stop making
objects. Both of these flags are set within <B>finalize(&#160;)</B>, which is
called during garbage collection.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Two other <B>static</B> variables,
<B>created</B> and <B>finalized</B>, keep track of the number of <B>obj</B>s
created versus the number that get finalized by the garbage collector. Finally,
each <B>Chair</B> has its own (non-<B>static</B>) <B>int</B> <B>i</B> so it can
keep track of what number it is. When <B>Chair</B> number 47 is finalized, the
flag is set to <B>true</B> to bring the process of <B>Chair </B>creation to a
stop.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">All this happens in
<B>main(&#160;)</B>, in the loop</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>    <font color=#0000ff>while</font>(!Chair.f) {
      <font color=#0000ff>new</font> Chair();
      <font color=#0000ff>new</font> String(<font color=#004488>"To take up space"</font>);
    }</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">You might wonder how this loop
could ever finish, since there&#8217;s nothing inside that changes the value of
<B>Chair.f</B>. However, the <B>finalize(&#160;)</B> process will, eventually,
when it finalizes number 47.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"> The creation of a <B>String</B>
object during each iteration is simply extra garbage being created to encourage
the garbage collector to kick in, which it will do when it starts to get nervous
about the amount of memory available.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">When you run the program, you
provide a command-line argument of &#8220;before&#8221; or &#8220;after.&#8221;
The &#8220;before&#8221; argument will call the <B>System.gc(&#160;)</B> method
(to force execution of the garbage collector) along with the
<B>System.runFinalization(&#160;)</B> method to run the finalizers. These
methods were available in <A NAME="Index302"></A>Java 1.0, but the
<A NAME="Index303"></A><B>runFinalizersOnExit(&#160;)</B> method that is invoked
by using the &#8220;after&#8221; argument is available only in Java
1.1<A NAME="Index304"></A></FONT><A NAME="fnB19" HREF="#fn19">[19]</A><FONT FACE="Georgia">
and beyond. (Note you can call this method any time during program execution,
and the execution of the finalizers is independent of whether the garbage
collector runs).</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The preceding program shows that,
in Java 1.1, the promise that finalizers will always be run holds true, but only
if you explicitly force it to happen yourself. If you use an argument that
isn&#8217;t &#8220;before&#8221; or &#8220;after&#8221; (such as
&#8220;none&#8221;), then neither finalization process will occur, and
you&#8217;ll get an output like this:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>Created 47
Beginning to finalize after 8694 Chairs have been created
Finalizing Chair #47, Setting flag to stop Chair creation
After all Chairs have been created:
total created = 9834, total finalized = 108
bye!</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Thus, not all finalizers get called
by the time the program
completes.</FONT><A NAME="fnB20" HREF="#fn20">[20]</A><FONT FACE="Georgia">
To force finalization to happen, you can call <B>System.gc(&#160;)</B> followed
by <B>System.runFinalization(&#160;)</B>. This will destroy all the objects that
are no longer in use up to that point. The odd thing about this is that you call
<B>gc(&#160;)</B> <I>before</I> you call <B>runFinalization(&#160;)</B>, which
seems to contradict the Sun documentation, which claims that finalizers are run
first, and then the storage is released. However, if you call
<B>runFinalization(&#160;)</B> first, and then <B>gc(&#160;)</B>, the finalizers
will not be executed.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">One reason that Java
1.1<A NAME="Index305"></A> might default to skipping finalization for all
objects is because it seems to be expensive. When you use either of the
approaches that force garbage collection you might notice longer delays than you
would without the extra
finalization.</FONT><A NAME="_Toc375545284"></A><A NAME="_Toc408018486"></A><BR></P></DIV>
<A NAME="Heading149"></A><FONT FACE = "Verdana"><H2 ALIGN="LEFT">
Member initialization</H2></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Java goes out of its way to
guarantee that any variable is properly initialized before it is used. In the
case of variables that are defined locally to a method, this guarantee comes in
the form of a compile-time error. So if you say:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>  <font color=#0000ff>void</font> f() {
    <font color=#0000ff>int</font> i;
    i++;
  }</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">You&#8217;ll get an error message
that says that <B>i</B> might not have been initialized. Of course, the compiler
could have given <B>i</B> a default value, but it&#8217;s more likely that this
is a programmer error and a default value would have covered that up. Forcing
the programmer to provide an initialization value is more likely to catch a
bug<A NAME="Index306"></A><A NAME="Index307"></A><A NAME="Index308"></A>.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">If a
<A NAME="Index309"></A><A NAME="Index310"></A><A NAME="Index311"></A>primitive
is a data member of a class, however, things are a bit different. Since any
method can initialize or use that data, it might not be practical to force the
user to initialize it to its appropriate value before the data is used. However,
it&#8217;s unsafe to leave it with a garbage value, so each primitive data
member of a class is guaranteed to get an initial value. Those values can be
seen here:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: InitialValues.java</font>
<font color=#009900>// Shows default initial values</font>

<font color=#0000ff>class</font> Measurement {
  <font color=#0000ff>boolean</font> t;
  <font color=#0000ff>char</font> c;
  <font color=#0000ff>byte</font> b;
  <font color=#0000ff>short</font> s;
  <font color=#0000ff>int</font> i;
  <font color=#0000ff>long</font> l;
  <font color=#0000ff>float</font> f;
  <font color=#0000ff>double</font> d;
  <font color=#0000ff>void</font> print() {
    System.out.println(
      <font color=#004488>"Data type      Inital value\n"</font> +
      <font color=#004488>"boolean        "</font> + t + <font color=#004488>"\n"</font> +
      <font color=#004488>"char           "</font> + c + <font color=#004488>"\n"</font> +
      <font color=#004488>"byte           "</font> + b + <font color=#004488>"\n"</font> +
      <font color=#004488>"short          "</font> + s + <font color=#004488>"\n"</font> +
      <font color=#004488>"int            "</font> + i + <font color=#004488>"\n"</font> +
      <font color=#004488>"long           "</font> + l + <font color=#004488>"\n"</font> +
      <font color=#004488>"float          "</font> + f + <font color=#004488>"\n"</font> +
      <font color=#004488>"double         "</font> + d);
  }
}

<font color=#0000ff>public</font> <font color=#0000ff>class</font> InitialValues {
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> main(String[] args) {
    Measurement d = <font color=#0000ff>new</font> Measurement();
    d.print();
    <font color=#009900>/* In this case you could also say:
    new Measurement().print();
    */</font>
  }
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The output of this program
is:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>Data type      Inital value
<font color=#0000ff>boolean</font>        <font color=#0000ff>false</font>
<font color=#0000ff>char</font>
<font color=#0000ff>byte</font>           0
<font color=#0000ff>short</font>          0
<font color=#0000ff>int</font>            0
<font color=#0000ff>long</font>           0
<font color=#0000ff>float</font>          0.0
<font color=#0000ff>double</font>         0.0</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The <B>char</B> value is a null,
which doesn&#8217;t print.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">You&#8217;ll see later that when
you define an object handle inside a class without initializing it to a new
object, that handle is given a value of null.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">You can see that even though the
values are not specified, they automatically get initialized. So at least
there&#8217;s no threat of working with uninitialized
variables.</FONT><A NAME="_Toc375545285"></A><A NAME="_Toc408018487"></A><BR></P></DIV>
<A NAME="Heading150"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Specifying initialization</H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">What happens if you want to give a
variable an initial value? One direct way to do this is simply to assign the
value at the point you define the variable in the class. (Notice you cannot do
this in C++, although C++ novices always try.) Here the field definitions in
class <B>Measurement</B> are changed to provide initial values:
<A NAME="Index312"></A><A NAME="Index313"></A></FONT><BR></P></DIV>

<BLOC

⌨️ 快捷键说明

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