About Question enthuware.ocpjp.v8.2.1584 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

mtrikannad
Posts: 11
Joined: Mon Jan 18, 2016 4:35 pm
Contact:

About Question enthuware.ocpjp.v8.2.1584 :

Post by mtrikannad »

I somehow dont seem to get this

c:\\personal\\.\\photos\\..\\readme.txt
Is the same as
c:\\personal\readme.txt ( The .. after photos should take you to personal ).

and relative to c:\\personal\\index.html , it is actually in the same directory. I am looking at it if I was doing a cd with the paths. Why does it evaluate to ..\..\..\..\index.html

I somehow to seem to have a block getting this.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1584 :

Post by admin »

Did you read the detailed explanation? Please let me know which part is causing you trouble so that we can explain further.
Paul.
If you like our products and services, please help us by posting your review here.

mtrikannad
Posts: 11
Joined: Mon Jan 18, 2016 4:35 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1584 :

Post by mtrikannad »

I understand the explanation and I dont dispute that is how it works. I am just mentioning the fact that when you actually change directories using a command prompt, the result is different.Does that make sense ?

c:\\personal\\.\\photos\\..\\readme.txt

cd c:\\personal\\.\\photos\\..\\

now Iam in c:\personal with a file readme.txt

c:\\personal\\index.html is in the same directory.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1584 :

Post by admin »

I am still not sure what is it that you are confused about but here are some thoughts anyway:

When you do this on the command line, the command line actually resolves the whole thing then and there. In other words, when you do cd c:\\personal\\.\\photos\\..\\, the command lines resolves the dots and dot dots and takes you to c:\personal.

The Java File object doesn't do any resolution, if you create a File object with cd c:\\personal\\.\\photos\\..\\, it remains as it is. i.e. it keeps the dots and dot dots intact inside the File object.
If you like our products and services, please help us by posting your review here.

mtrikannad
Posts: 11
Joined: Mon Jan 18, 2016 4:35 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1584 :

Post by mtrikannad »

Thanks for the explanation.

javabean68
Posts: 31
Joined: Wed Mar 16, 2016 8:38 am
Contact:

Re: About Question enthuware.ocpjp.v8.2.1584 :

Post by javabean68 »

Hi all,

I didn't understand this step:
=>c:\\personal\\.\\photos\\.. + ..\..\..\index.html =>c:\\personal\\.\\photos + ..\..\index.html

I would have expected:
=>c:\\personal\\.\\photos\\.. + ..\..\..\index.html =>c:\\ + ..\..\index.html

instead.

Could you please explain?

Thank you very much
Bye
Fabio

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1584 :

Post by admin »

Why do you think c:\\personal\\.\\photos\\.. will reduce to c:\\ ?
If you like our products and services, please help us by posting your review here.

javabean68
Posts: 31
Joined: Wed Mar 16, 2016 8:38 am
Contact:

Re: About Question enthuware.ocpjp.v8.2.1584 :

Post by javabean68 »

Hallo,

because it is connected to ' ../../..'

:roll:

javabean68
Posts: 31
Joined: Wed Mar 16, 2016 8:38 am
Contact:

Re: About Question enthuware.ocpjp.v8.2.1584 :

Post by javabean68 »

Hallo,

your response is anyway right...so it is as . or .. are considered as ein Directory .

The result is the same with:
public class Relativize
{
public static void main(String[] args) {
Path p1 = Paths.get("/personal/instead./photos/instead../readme.txt");
Path p2 = Paths.get("/personal/index.html");
Path p3 = p1.relativize(p2);
System.out.println(p3);
}
}

../../../../index.html

A little counterintuitive :-)

Thank you
Fabio

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1584 :

Post by admin »

You wrote:
I would have expected:
=>c:\\personal\\.\\photos\\.. + ..\..\..\index.html =>c:\\ + ..\..\index.html
This implies that you think
c:\\personal\\.\\photos\\.. resolves to c:\\
and
..\..\..\index.html resolves to ..\..\index.html

which, to me, doesn't make sense because there is only one dot dot in c:\\personal\\.\\photos\\.., and that will cancel out the path element just behind it, i.e. /photos. Therefore, c:\\personal\\.\\photos\\.. will shorten to c:\\personal\\.\\ and since dot is a reference to the same directory, it can be removed without affecting anything else and therefore that makes it c:\\personal\\ and not c:\\
If you like our products and services, please help us by posting your review here.

JavaNerd
Posts: 3
Joined: Sun Jul 24, 2016 1:04 am
Contact:

Re: About Question enthuware.ocpjp.v8.2.1584 :

Post by JavaNerd »

Hi,

Could you help me make sense out of this:

Code: Select all

scala> val p1 = Paths.get("/personal/./photos/../readme.txt")
p1: java.nio.file.Path = /personal/./photos/../readme.txt

scala> val p2 = Paths.get("/personal/index.html")
p2: java.nio.file.Path = /personal/index.html

scala> p1.relativize(p2)
res6: java.nio.file.Path = ../../../../index.html

scala> p1.normalize.relativize(p2)
res7: java.nio.file.Path = ../index.html
I adapted the code to Linux environment, so "c:\\something" became "/something".

What's confusing me is why normalized and non-normalized versions produce different results.

Also, it seems to break the invariant:

Code: Select all

scala> p1.resolve(p1.relativize(p2)).normalize
res10: java.nio.file.Path = /index.html

scala> p1.resolve(p1.normalize.relativize(p2)).normalize
res11: java.nio.file.Path = /personal/index.html

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1584 :

Post by admin »

p1 and p1.normalize contain different path elements. So why do you think relativize should return the same value?
If you like our products and services, please help us by posting your review here.

ssszzz
Posts: 13
Joined: Wed Jun 14, 2017 1:56 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1584 :

Post by ssszzz »

Hello.
I still do not understand something...

Javadoc says
Relativization is the inverse of resolution. This method attempts to construct a relative path that when resolved against this path, yields a path that locates the same file as the given path.
Lets check:

Code: Select all

    public static void main(String... strings) {
        Path p1 = Paths.get("c:\\personal\\.\\photos\\..\\readme.txt");
        Path p2 = Paths.get("c:\\personal\\index.html");
        System.out.println("p1=\t" + p1);
        System.out.println("p2=\t" + p2);

        Path p3 = p1.relativize(p2);
        System.out.println("p3=\t" + p3 + "\t/*this is a relative path, a result of p1.relativize(p2)*/");

        System.out.println("p1.resolve(p3)=\t" + p1.resolve(p3));
        System.out.println("(p1.resolve(p3)).normalize()=\t" + (p1.resolve(p3)).normalize());
    }
The output:

Code: Select all

p1=	c:\personal\.\photos\..\readme.txt
p2=	c:\personal\index.html
p3=	..\..\..\..\index.html	/*this is a relative path, a result of p1.relativize(p2)*/
p1.resolve(p3)=	c:\personal\.\photos\..\readme.txt\..\..\..\..\index.html
(p1.resolve(p3)).normalize()=	c:\index.html
So, p2 != p1.resolve(p3). Question N1: Why ?
Maybe it is because of
Where both paths have a root component then it is implementation dependent if a relative path can be constructed.
Both paths have root components, right? So, question N2: is the answer ('..\..\..\..\index.html') really correct to this puzzle?

Or maybe it is related to JDK-6925169 (but i could not find description of this bug)?

tested on build 1.8.0_131-b11

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1584 :

Post by admin »

On a windows system, a path starting with c:\\ is a path with root component. But not so on a *nix system.
So if you use paths with c:\\ and try p.relativize(p.resolve(q)).equals(q) on windows, it may not print true. But on *nix, it will print true.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

ssszzz
Posts: 13
Joined: Wed Jun 14, 2017 1:56 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1584 :

Post by ssszzz »

operation system is not mentioned in the question, although the paths look like windows paths.
So we have to think it does not matter where code is running.
So if you use paths with c:\\ and try p.relativize(p.resolve(q)).equals(q) on windows, it may not print true.

- of course and everywhere, since according javadoc for this expression to be fulfilled the path q should not have a root component and both paths should be normalized.
it is not our case.
We have two absolute paths and one of them is not normalized.
Moreover, we can just remove prefix "c:\\" from the example of code above to make them not absolute. What do we get? The same answer but without prefix "c:\\" (tested on windows)

So my questions remain in force.
1) why p.resolve(p.relativize(q)).normilize() != q.normilize() ? Note, it is not the same as expression above.
I think it is an equivalent of what javadoc says:

Code: Select all

Relativization is the inverse of {@link #resolve(Path) resolution}.
- it is the second line of method description.
Or I am not right and the expression and statement are not equivalent? Then how I can test this statement ('relativization is the inverse of resolution') ?
or maybe it is not true for two absolute windows-style paths where one of them has '..' inside (i.e. maybe it is mythical JDK-6925169)?

2) or maybe if it is not true since
Where both paths have a root component then it is implementation dependent if a relative path can be constructed
? If so, how we can calculate the true answer at all? It is unpredictable on windows.
What about linux? In the discussion above said that there is also problem here - instead '/personal/index.html' we get '/index.html'. I.e. almost the same answer,

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1584 :

Post by admin »

Don't use absolute path on windows for p and q and p.relativize(p.resolve(q)).equals(q) will print true.
If you like our products and services, please help us by posting your review here.

ssszzz
Posts: 13
Joined: Wed Jun 14, 2017 1:56 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1584 :

Post by ssszzz »

what about p.resolve(p.relativize(q)).equals(q)?
Observe that if you append this path to p1, you will get p2.
(this is from puzzle explanation)
so, p1.resolve(p3).equals(p2) should return true? for not absolute path? or for absolute also? or for windows? or everywhere?

By the way
Don't use absolute path on windows for p and q and p.relativize(p.resolve(q)).equals(q) will print true.
Lets check:

Code: Select all

    public static void main(String... strings) {
        String r = "\\"; // "c:\\"; // "";
        Path p1 = Paths.get(r + "personal\\.\\photos\\..\\readme.txt");
        Path p2 = Paths.get(r + "personal\\index.html");
        System.out.println("p1=\t" + p1 + "; absolute=" + p1.isAbsolute() + "; root=" + p1.getRoot());
        System.out.println("p2=\t" + p2 + "; absolute=" + p2.isAbsolute() + "; root=" + p2.getRoot());

        Path p3 = p1.relativize(p2);
        System.out.println("p3=\t" + p3 + "\t/*this is a relative path, a result of p1.relativize(p2)*/");

        System.out.println("p1.resolve(p3).equals(p2) = " + p1.resolve(p3).equals(p2)); // output: false

        System.out.println("p1.resolve(p1.relativize(p2)).equals(p2) = " + p1.resolve(p1.relativize(p2)).equals(p2)); // output: false
        System.out.println("p1.relativize(p1.resolve(p2)).equals(p2) = " + p1.relativize(p1.resolve(p2)).equals(p2));

        System.out.println("p1.resolve(p3)=\t" + p1.resolve(p3));
        System.out.println("(p1.resolve(p3)).normalize()=\t" + (p1.resolve(p3)).normalize());
    }
output:

Code: Select all

p1=	\personal\.\photos\..\readme.txt; absolute=false; root=\
p2=	\personal\index.html; absolute=false; root=\
p3=	..\..\..\..\index.html	/*this is a relative path, a result of p1.relativize(p2)*/
p1.resolve(p3).equals(p2) = false
p1.resolve(p1.relativize(p2)).equals(p2) = false
p1.relativize(p1.resolve(p2)).equals(p2) = false
p1.resolve(p3)=	\personal\.\photos\..\readme.txt\..\..\..\..\index.html
(p1.resolve(p3)).normalize()=	\index.html
So, no. it will not print true for two not absolute paths if they have roots. but if no roots - yes, p.relativize(p.resolve(q)).equals(q) == true.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1584 :

Post by admin »

Sorry, I mistyped. I meant path with roots in my head but wrote absolute paths instead. Sorry about about that.
The following does print true:
Path p1 = Paths.get("personal\\.\\photos\\..\\readme.txt");
Path p2 = Paths.get("personal\\index.html");
System.out.println(p1.relativize(p1.resolve(p2)).equals(p2));

-Paul.
If you like our products and services, please help us by posting your review here.

ssszzz
Posts: 13
Joined: Wed Jun 14, 2017 1:56 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1584 :

Post by ssszzz »

how to append path p3 to p1 to get p2? I mean programmatically, not like in explanation

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1584 :

Post by admin »

Code: Select all

        Path p1 = Paths.get("personal\\.\\photos\\..\\readme.txt");
        System.out.println("p1=" + p1.normalize());
        Path p2 = Paths.get("personal\\index.html");
        System.out.println("p2=" + p2.normalize());
        Path p3 = p1.normalize().relativize(p2);
	System.out.println("p3="+p3);
	System.out.println("p="+p1.resolve(p3).normalize());
Output:

Code: Select all

p1=personal\readme.txt
p2=personal\index.html
p3=..\index.html
p=personal\index.html
So what is the issue?
If you like our products and services, please help us by posting your review here.

ssszzz
Posts: 13
Joined: Wed Jun 14, 2017 1:56 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1584 :

Post by ssszzz »

in the line Path p3 = p1.normalize().relativize(p2);
p1.normalize() = ''personal\\readme.txt"
and the answer is "p3=..\index.html".
It is not correct. The correct is "p3=..\..\..\..\index.html"

Code: Select all

        Path p1 = Paths.get("personal\\.\\photos\\..\\readme.txt");
        Path p2 = Paths.get("personal\\index.html");
        Path p3 = p1.relativize(p2);
        System.out.println("p3=" + p3); // output: p3=..\..\..\..\index.html
        System.out.println("p=" + p1.resolve(p3).normalize()); // output: p=..\..\index.html
and, it seems, the issue is in java
I do not know how much you can trust the following explanation, but people are talking about a bug in Path#relativize https://stackoverflow.com/questions/377 ... 2#37749932 (mysterious bug JDK-6925169)
The Path implementation does not handle . and .. components correctly in relativize, but treats them like any other path component.
So, in my opinion, there is either a bug, or poor documentation. And the question is about edge case, with not correct explanation of answer.

Ariskatsaris
Posts: 1
Joined: Mon Feb 19, 2018 8:30 am
Contact:

Re: About Question enthuware.ocpjp.v8.2.1584 :

Post by Ariskatsaris »

In java 9 I tested the code exactly:

Code: Select all

Path p1 = Paths.get("c:\\personal\\.\\photos\\..\\readme.txt");
Path p2 = Paths.get("c:\\personal\\index.html");
Path p3 = p1.relativize(p2);
System.out.println(p3);
And it prints
..\index.html

namely answer (c) which is what I would have expected to.

I'm guessing that the implementation changed between Java 8 and Java 9. I think perhaps a note should be added to this effect, so that it doesn't confused people.

Where I'm concerned the Java 9 implementation is very clearly the correct and proper one, and the Java 7/Java 8 are horrible bugs. Consider for example the following snippet of code:

Code: Select all

        Path p1 = Paths.get("c:\\personal\\.\\photos\\");
        Path p2 = Paths.get("c:\\personal\\photos");
        Path p3 = p1.relativize(p2);
        System.out.println("Relative: " + p3);

        Path p4 = p1.resolve(p3);
        System.out.println("Resolved: " + p4);
        System.out.println("Normalized: " + p4.normalize());
In Java 9 it prints the reasonable:
Relative:
Resolved: c:\personal\.\photos
Normalized: c:\personal\photos

In Java 7 and Java 8 it prints the horrible and just plain wrong:
Relative: ..\..\photos
Resolved: c:\personal\.\photos\..\..\photos
Normalized: c:\photos

shamran99
Posts: 15
Joined: Wed May 10, 2017 2:49 am
Contact:

Re: About Question enthuware.ocpjp.v8.2.1584 :

Post by shamran99 »

Hi,

Can I understand the relativize() function's behavior like..
whatever the path elements('.' , '..' , 'file.txt' or 'dir') are included in the path object, the relativize considers it as a directory.
Therefore it relativize the path with '..'?

Regards,
Shamran.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1584 :

Post by admin »

Yes, that seems correct.
If you like our products and services, please help us by posting your review here.

__JJ__
Posts: 125
Joined: Thu Jul 05, 2018 6:44 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1584 :

Post by __JJ__ »

p1.relativize(p2) => how to get to p2 from p1.
It might be worth pointing out that relativize DOES NOT normalize before working out the result, and consequently
p1.normalize().relativize(p2) will potentially give you a completely different result to p1.relativize(p2):

Code: Select all

        Path p1 = Paths.get("c:\\personal\\.\\photos\\..\\readme.txt");
        Path p2 = Paths.get("c:\\personal\\index.html");
        Path p3 = p1.relativize(p2);
        System.out.println(p3); //..\..\..\..\index.html <== actual answer
        System.out.println(p1.normalize().relativize(p2)); //..\index.html <== what I thought would be the answer..

Post Reply

Who is online

Users browsing this forum: No registered users and 48 guests