What is the output of the following application?
package mypkg; //from ww w .ja v a 2s . c o m import java.nio.file.*; public class Main { public void m() { Path p1 = Paths.get("mypkg/./p1/../m1.data") .normalize(); Path p2 = Paths.get("./mypkg/../p2/"); p2 = p2.subpath(0, 2) .resolve("m1.data") .normalize(); System.out.print(p1.equals(p2) ? "Same!" : "Different!"); } public static void main(String... emerald) { Main s = new Main(); s.m(); } }
B.
The program compiles and runs without issue, making Options C and D incorrect.
The first variable, p1, is created with normalize()
being applied right away, leading to a value of mypkg/m1.data.
The second variable, p2, starts with a value of ./mypkg/../p2/.
The subpath()
call reduces it to its first two components, ./mypkg.
The resolve()
method then appends m1.data, resulting in a value of ./mypkg/m1.data.
Finally, normalize()
further reduces the value to mypkg/m1.data.
Since this matches our first Path, the program prints Same!, and Option B is the correct answer.