What is the output of the following code snippet, assuming none of the files referenced exist within the file system?
Path t1 = Paths.get("/p1/.././p2.exe"); Path t2 = Paths.get("/p2.exe"); Path t3 = t1.resolve(t2); /* w w w . j a v a 2 s. c o m*/ boolean b1 = t1.equals(t2); boolean b2 = t1.normalize().equals(t2); boolean b3 = Files.isSameFile(t1.normalize(),t2); boolean b4 = Files.isSameFile(t2,t3); System.out.print(b1+","+b2+","+b3+","+b4);
C.
The code compiles, so Option E is incorrect.
The first boolean expression returns false because the two Path expressions have different values and are therefore not equivalent.
On the other hand, the second boolean expression returns true.
If we normalize t1, it becomes /p2.exe, which is equivalent to the t2 variable in terms of equals()
.
The third boolean expression also returns true, even though the file does not exist.
The isSameFile()
method will avoid checking the file system if the two Path expressions are equivalent in terms of equals()
, which from the second boolean expression we know that they are.
That leaves the fourth boolean expression, which returns true.
Passing an absolute Path to resolve()
just returns it, so t2 and t3 are equivalent values.
Option C is the correct answer.
Note that if the Path values had not been equivalent in terms of equals()
for either of the last two boolean expressions, then the file system would have been accessed, and since none of the files exist, an exception would have been thrown at runtime.