Search code examples
javafilepath

The difference between Absolute path and Path with a root component in Java documentation of Path.resolve()


I have been learning about file system management in Java. I came across the documentation of the resolve method under Path interface. I did not understand the difference between a path being absolute and its having a root component. Aren't they the same?

From the documentation:

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.

I thought that they mean the same and searched on the net but found nothing.


Solution

  • On Unix systems: Yes. A path is absolute, if and only if it has a root component (which is always "/").

    On Windows, things get... ugly. There are 5(!) different types of paths. Those are:

    • absolute path (root component looks like "C:\" - drive letter plus root directory)
    • relative path (root component is empty)
    • directory relative path (root component is "\" - root directory only)
    • drive relative path (root component looks like "C:" - drive letter only)
    • UNC path (for network access)

    Only absolute paths and UNC paths are deemed "absolute" by WindowsPath.isAbsolute()