0

Given the following code

    Path p1 = Paths.get("\\photos\\vacation");
    Path p2 = Paths.get("\\yellowstone");
    System.out.println(p1.isAbsolute());  // false
    System.out.println(p2.isAbsolute());  // false
    System.out.println(p1.resolve(p2));  // \yellowstone

JavaDoc says:

public abstract Path resolve(Path other)

Resolve the given path against this path.

If the other parameter is an absolute path then this method trivially returns other.

If other is an empty path then this method trivially returns this path.

Otherwise this method considers this path to be a directory and resolves the given path against this path. In the simplest case, the given path does not have a root component, in which case this method joins the given path to this path and returns a resulting path that ends with the given path. Where the given path has a root component then resolution is highly implementation dependent and therefore unspecified.

Running on Windows the paths are not absolute, but its still retuns p2 as a result; I couldnt understand that behaviour. What I am missing?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
psyskeptic
  • 286
  • 2
  • 4
  • 17
  • They both say the path is not absolute, because the drive letter (or "root component" as the quoted text calls it) is missing. Since `p2` is fully qualified from the root of the drive, then only part of the result that would come from `p1` is the drive letter, and `p1` doesn't have that either, so result is same as `p2`. As it should be!! --- But answer this: If you didn't expect result to be same as `p2`, what did you expect result to be? – Andreas May 25 '17 at 16:58
  • What do you want to achieve with your code? – dpr May 25 '17 at 17:04
  • 1
    Notice it does not say “If *and only if* the other parameter is an absolute path then this method trivially returns other.” As you have observed, there are situations where it can return p2, even if p2 is not absolute. – VGR May 25 '17 at 17:42
  • This is a code from question of oracle OCP certification exam – psyskeptic May 27 '17 at 10:39

0 Answers0