下面是某本书上的一段程序代码,有个疑问如下,红色字体..
package day1;import java.io.ByteArrayInputStream;
import java.io.IOException;public class ByteArrayTester { /**
 * @param args
 */
public static void main(String[] args) {
// TODO Auto-generated method stub
byte[] buff=new byte[]{2,15,67,-1,-9,9};
ByteArrayInputStream in=new ByteArrayInputStream(buff,1,4);
int data=in.read();
while(data!=-1){
System.out.println(data+" ");
data=in.read();
}
try {
in.close();//ByteArrayInputStream的close()方法实际上不执行任何操作,那为什么要写呢?
还有in对象应该没有销毁吧,个人认为应该加上  in=null;

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}}

解决方案 »

  1.   


    package day1;import java.io.ByteArrayInputStream;
    import java.io.IOException;public class ByteArrayTester {    /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            byte[] buff=new byte[]{2,15,67,-1,-9,9};
            ByteArrayInputStream in=new ByteArrayInputStream(buff,1,4);
            int data=in.read();
            while(data!=-1){
                System.out.println(data+" ");
                data=in.read();
            }
            try {
                in.close();//ByteArrayInputStream的close()方法实际上不执行任何操作,那为什么要写呢?
    还有in对象应该没有销毁吧,个人认为应该加上  in=null; 
               
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }}
    上面的没标出来..再来下
      

  2.   

    ByteArrayInputStream的close()方法实际上不执行任何操作,那为什么要写呢?
    流用完了要关闭啊
    关闭了虚拟机才知道其已经没有价值了
    空闲的时候垃圾回收机制会回收的
    还有in对象应该没有销毁吧,个人认为应该加上  in=null;
    in=null;是显示的告诉虚拟机它已经没有价值了 可以回收 但什么时候回收是虚拟机决定的
    不用显示的写出来 在其没有价值的时候 虚拟机也会回收它的 其实in=null;这样的代码意义不大
      

  3.   

    那个只是关闭io流而已,和对象没关系
    你可以自己+上判断来调节
    不过我想问下lz
    你传输完毕以后不关闭,想继续传么?那为什么还要判断?
    如果不传了,为什么不关闭?等他报溢出的异常么?
    ================
    不懂你的想法
      

  4.   

    对于 ByteArrayInputStream 关不关并不影响 GC 回收其占用的资源,但要记得将引用置空(in = null;),以下是源代码及文档注释:

        /**
         * Closing a <tt>ByteArrayInputStream</tt> has no effect. The methods in
         * this class can be called after the stream has been closed without
         * generating an <tt>IOException</tt>.
         * <p>
         */
        public void close() throws IOException {
        }明确告诉你了,关了也对 ByteArrayInputStream 没任何影响,因为方法体是空的,当然调用 close 方法会耗用一星半点
    的完全可忽略的计算资源。那为什么还要实现此方法呢?多次一举?切听下面详解:

    我们通常使用的输入流一端是 Java 应用程序,而另一端通常是硬件设备或操作系统提供的本地资源(不在JVM管理范围内的系统资源),例如 FileInputStream 的另一端是OS的文件系统,Socket.getInputStream() 得到的输入流的另一端是网卡驱动和物理网络接口卡(NIC),当使用完此类输入流时,一般要显式地调用其 close 方法来释放本地资源。而 ByteArrayInputStream 的另一端是 JVM 管辖下的 byte[],当然不需要 close 方法。之所以 ByteArrayInputStream 提供了一个什么也不做的方法,一方面是因为其超类 InputStream 有此方法(方法体一样一行代码没有),所以即便 ByteArrayInputStream 不提供 close 方法也会从其超类继承下来 close 方法,而之所以要覆盖继承的方法而又不改变其功能是因为要在 ByteArrayInputStream 的文档注释中要明确地告诉你“Closing a ByteArrayInputStream has no effect. The methods inthis class can be called after the stream has been closed without generating an IOException.” 。
      

  5.   

    在写数据库程序的时,都有像in=null的语句,好让垃圾收集器把它回收,可能在这里写一下会好一些吧。