Why does Path.getParent() return null?

Paulo Levi i30817 at gmail.com
Fri Feb 7 12:37:14 PST 2014


I tried this little ideone test:
http://ideone.com/O8rDCZ

import java.util.*;
import java.lang.*;
import java.io.*;
import java.nio.file.Paths;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
        System.out.println(Paths.get("", "dir").toString().equals("dir"));
    }
}

Output: true

that is, the toString method returns dir. I'm not sure if inconsistent.




On Thu, Feb 6, 2014 at 8:53 PM, Alan Bateman <Alan.Bateman at oracle.com>wrote:

> On 06/02/2014 18:12, Paulo Levi wrote:
>
>> I'm curious about the reason for not having a special empty path for the
>> case where the parent doesn't exist instead of returning null.
>>
>> There is quite a bit of code on my experience that has to check for null,
>> when they only call get parent even once, while if it returned a special
>> null empy-space object they would never have to. Even Path.get("",
>> "somedir") ignores the whitespace so it could use that has a
>> representation. Or is the problem that that is already taken for current
>> directory in the path implementation (although i've found that it doesn't
>> have that effect on the get ).
>>
> The empty path is specified so that accessing it is equivalent to
> accessing the default directory.
>
> I didn't get your point about Paths.get("", "somedir") as the parameters
> are used to form a path-string. The javadoc details how empty Strings are
> handled. Are you suggesting there is an inconsistency?
>
>
>
>> Also, i'd like to know if that method will be annotated with a
>> @CheckForNull or similar on the jdk.
>>
> Sorry, there isn't an equivalent annotation in the JDK.
>
> -Alan
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.openjdk.java.net/pipermail/nio-discuss/attachments/20140207/2b151ad8/attachment.html 


More information about the nio-discuss mailing list