Page 1 of 1

About Question enthuware.ocpjp.v7.2.1608 :

Posted: Tue Jun 21, 2016 4:40 am
by dieterdde
This one caught me off guard/confuses me. I thought initially that both paths p1 & p2 were relative paths because neither start with a root, which is c: on windows and / on unix, right?

But given that assumption, p1.resolve(p2) would have been simply tacking on the relative p2 path onto p1 which would be: \\photos\\vacation\\yellowstone

However given that none of the options had this answer, and knowing that when you pass an absolute path into the argument, it will return the absolute argument, I concluded that \\ means these are absolute paths.

Hence resolve returning the path passed being \\yellowstone which turns into \yellowstone as a String output.

But I see here Paul is saying these paths are not absolute paths but relative paths. This seems to be confirmed, as in my testing, i get null for Paths.get("\\test").getRoot()

I'm very confused here, if these are truly not absolute paths (which getRoot() confirms), why isn't p2 then not simply tacked onto p1?

In the K&B book for OCA/OCP7 prep, there is an example like this:

Path relative = Paths.get("dir");
Path file = Paths.get("Model.pdf");
relative.resolve(file) -> dir/Model.pdf
file.resolve(relative) -> Model.pdf/dir

Both the relative path and the file are relative paths, right? Why do they get tacked on and not in the mock exam question?

Given that "\\test" is a relative path (as I understand from this thread), is it any different from just "test" ?

Cheers!
Dieter

EDIT, in fact on my system, Mac OS X I get this:
Path p1 = Paths.get("\\test");
Path p2 = Paths.get("\\er");
p1.resolve(p2) -> \test/\er

What the.... ?

Re: About Question enthuware.ocpjp.v7.2.1608 :

Posted: Fri Aug 12, 2022 10:32 am
by aPerson
Correct me if I'm wrong, but I believe the "argument to resolve" in the explanation refers to p1, whereas "argument" refers to p2 (in case it confuses anyone else). Also, I made the following overview to help me understand:

"c:\\photos\\vacation" + "" --> c:\photos\vacation
"c:\\photos\\vacation" + "yellowstone" --> c:\photos\vacation\yellowstone
"c:\\photos\\vacation" + "\\yellowstone" --> c:\yellowstone
"c:\\photos\\vacation" + "c:\\yellowstone" --> c:\yellowstone

"\\photos\\vacation" + "" --> \photos\vacation
"\\photos\\vacation" + "yellowstone" --> \photos\vacation\yellowstone
"\\photos\\vacation" + "\\yellowstone" --> \yellowstone
"\\photos\\vacation" + "c:\\yellowstone" --> c:\yellowstone

Re: About Question enthuware.ocpjp.v7.2.1608 :

Posted: Sat Aug 13, 2022 4:45 am
by admin
Yes, this is a bit weird.
1. Since the given paths use \ (i.e. backslash), it is fair to assume that windows paths are being used. Although it is possible to have such paths on Unix based systems also, for the purpose of the exam, consider only paths contains / (i.e. forward slash) to be unix paths.
2. On windows, paths starting with root such as c:\ are absolute paths. Therefore, in the given question, neither of the paths are absolute paths.

However, it looks like when you start a windows path with a \, the implementation is considering it as an absolute path. This is proven by the following code:
(I am running the code from c:\temp directory)

Code: Select all

   Path p2 = Paths.get("yellowstone");
   System.out.println(p2.toAbsolutePath()); //prints C:\temp\yellowstone

   p2 = Paths.get("\\yellowstone");
   System.out.println(p2.toAbsolutePath()); //prints C:\yellowstone
This explains the result of p1.resolve(p2) in the given question.

The bottom line is that for the purpose of the exam it is best to use unix based paths for experimentation and learning how these methods work and not waste time on this. We will update this question accordingly.