码迷,mamicode.com
首页 > 编程语言 > 详细

java.io.File在作为参数传递时体现值引用的特点

时间:2015-03-20 15:51:21      阅读:334      评论:0      收藏:0      [点我收藏+]

标签:

File是对象引用,但在作为参数传递时体现值引用的特点,即 在方法中被重新赋值,此赋值操作仅在当前方法中有效。

public class FileUtils {
    public static void main(String[] args) {
        String filepath=FileUtils.class.getResource("/files/t1/example/test.xlsx").getPath();
        System.out.println(filepath);
        FileUtils fileUtils=new FileUtils();
        File oldFile=new File(filepath);
        System.out.println("oldFile:"+oldFile.exists()+":"+oldFile.getAbsolutePath());
        fileUtils.del(oldFile);
        System.out.println("oldFile:"+oldFile.exists()+":"+oldFile.getAbsolutePath());
    }

    public void del(File oldFile){
        
        if (oldFile.exists()) {
            System.out.println(oldFile.getAbsolutePath());
            oldFile.delete();
        }
        String path=FileUtils.class.getClass().getResource("/files/t1/example/test2.xlsx").getPath();
        System.out.println(path);
        File newFile=new File(path);
        System.out.println("newFile:"+newFile.exists()+":"+newFile.getAbsolutePath());
        oldFile=newFile;
    }
}

output:

/D:/workspace/bin/files/t1/example/test.xlsx
oldFile:true:D:\workspace\bin\files\t1\example\test.xlsx
D:\workspace\bin\files\t1\example\test.xlsx
/D:/workspace/bin/files/t1/example/test2.xlsx
newFile:true:D:\workspace\bin\files\t1\example\test2.xlsx
oldFile:false:D:\workspace\bin\files\t1\example\test.xlsx//main方法中File对象还是以前的

 

java.io.File在作为参数传递时体现值引用的特点

标签:

原文地址:http://www.cnblogs.com/softidea/p/4353542.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!