package?testProgramming;
import?java.io.File;
public?class?TestMethod?{
public?static?void?main(String[]?args){
String?f1=new?String("F:\\javaTest\\Paint.txt");
String?f2="F:\\javaTest\\Paint.txt";
File?file1=new?File(f1);
File?file2=new?File(f2);
System.out.println("F:\\javaTest\\Paint.txt"==file1.getPath());//false
System.out.println("F:\\javaTest\\Paint.txt".hashCode()==file1.getPath().hashCode());//true
System.out.println(f1.toString()==file1.getPath());//true
System.out.println(f1.toString().hashCode()==file1.getPath().hashCode());//true
System.out.println("F:\\javaTest\\Paint.txt"==file2.getPath());//true
System.out.println("F:\\javaTest\\Paint.txt".hashCode()==file2.getPath().hashCode());//true
System.out.println(f2.toString()==file2.getPath());//true
System.out.println(f2.toString().hashCode()==file2.getPath().hashCode());//true
System.out.println(file2.equals(file1));//true
System.out.println(file2.hashCode()==file1.hashCode());//true
}
}
第一:存變量是按照其hashcode值存的,f.getPath本質(zhì)是一個(gè)字符串,打印出來(lái)的也是一個(gè)字符串;因此相等
第二:file1.equals(file2)為true的原因是其路徑f1和f2表示的是同一個(gè)字符串,因此hashcode值相等,
第三:第一行打印為false的原因:file1.getPath()表示的是一個(gè)String類(lèi)對(duì)象f1;因此調(diào)用==比較的時(shí)候其代表自己的地址而不是值,因此為false