About Question enthuware.ocpjp.v8.2.1610 :

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

Moderator: admin

Post Reply
schchen2000
Posts: 106
Joined: Mon Mar 28, 2016 11:36 pm
Contact:

About Question enthuware.ocpjp.v8.2.1610 :

Post by schchen2000 »

Path p2 = p1.resolveSibling("\\clients.dat");

This will set p2 to c:\clients.dat, which is not what you want.
Hi,

Do you mind sharing the steps involved from starting with the argument "\\clients.dat" and ending up with the result "c:\clients.dat"? This is from your 1st answer choice.

A separate question: Why is the given information "Assume that the current directory for the program when it runs is c:\code." relevant in this question?

Thanks.

Schmichael

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

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

Post by admin »

1. Since the current directory( c:\code) starts with c:, that means the root of the file system is c:.

2. When any Path object starts with \\ or / , that means it starts from root. Therefore, when you resolve \\clients.dat it will give you c:\clients.dat
If you like our products and services, please help us by posting your review here.

schchen2000
Posts: 106
Joined: Mon Mar 28, 2016 11:36 pm
Contact:

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

Post by schchen2000 »

admin wrote: 2. When any Path object starts with \\ or / , that means it starts from root. Therefore, when you resolve \\clients.dat it will give you c:\clients.dat
What do you mean when you said ".... it starts from root....."?

My understanding is the root in Windows is c: and in Linux, the root is /.

In your question,
Path p1 = Paths.get("c:\\company\\records\\customers.dat");
p1 holds a path, whose root directory is c:.

If we are to use
Path p2 = p1.resolveSibling("\\clients.dat");
I understand the quote in red above is not the right answer choice but let's suppose it is for a moment.

I tried running the following code with \\ in Linux and it says \\ is not the root but if you change \\zooinfo in the following code to /zooinfo and run it again in Linux, then it says / is the root.

Are you insinuating \\ is an alternative way to represent the root in Windows in addition to c: being the root directory in Windows?

What intrigues me is that how Java compiler figures out when the root in Windows, i.e. c: and the root in Linux, i.e. / are present and used in the same program. How does Java compiler resolve this issue in cases that involve resolveSibling() and relativize() methods?

Each of these methods works with 2 paths and one path being an absolute path in Windows, i.e. the path that starts with c: and the other path being an absolute path in Linux, i.e. the path that starts with /.

I would love you hear what you'd have to say on this.

I understand that Java's motto is compile once and run anywhere.

Code: Select all


import java.nio.file.*;

public class resolveSiblingTest{

        public static void main(String... args){

                Path path = Paths.get("\\zooinfo\\November\\employees.txt");

                System.out.println("\nIs absolute: " + path.isAbsolute() + "\n");

                System.out.println("\nRoot: " + path.getRoot() + "\n");

        }

}

Thanks.

Schmichael

schchen2000
Posts: 106
Joined: Mon Mar 28, 2016 11:36 pm
Contact:

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

Post by schchen2000 »

I'm not sure if my last post was brought to the attention of Admin or Paul. Thanks.

Schmichael

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

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

Post by admin »

Can you try the following code on windows and tell me what inferences can you draw from the output?

Path p = Paths.get("\\temp");
System.out.println(p.toAbsolutePath());

p = Paths.get("/temp");
System.out.println(p.toAbsolutePath());

p = Paths.get("c:\\temp");
System.out.println(p.toAbsolutePath());

(Assuming that you are running the program from any directory in c:\)
-Paul.
If you like our products and services, please help us by posting your review here.

schchen2000
Posts: 106
Joined: Mon Mar 28, 2016 11:36 pm
Contact:

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

Post by schchen2000 »

admin wrote:Can you try the following code on windows and tell me what inferences can you draw from the output?

Path p = Paths.get("\\temp");
System.out.println(p.toAbsolutePath());

p = Paths.get("/temp");
System.out.println(p.toAbsolutePath());

p = Paths.get("c:\\temp");
System.out.println(p.toAbsolutePath());

(Assuming that you are running the program from any directory in c:\)
-Paul.
Paul,

Thanks for your response. I'm afraid I won't be able to run those on Windows as I've used only Linux OS for a number of years.

I did attempt to run these on a Windows simulator through a browser as I do not wish to install a virtual machine on my Linux system.

I believe I have run similar cases in my 3rd to last post. If I'm not asking you much, would you please tell me what they are?

This is what's given by Oracle about toAbsolutePath() method.
Path toAbsolutePath()

Returns a Path object representing the absolute path of this path.

If this path is already absolute then this method simply returns this path. Otherwise, this method resolves the path in an implementation dependent manner, typically by resolving the path against a file system default directory. Depending on the implementation, this method may throw an I/O error if the file system is not accessible.

Returns:
a Path object representing the absolute path
The following are my educated guess:

For

p = Paths.get("c:\\temp");
System.out.println(p.toAbsolutePath());

I think it will print something like c:\temp in Windows as anything that starts with c: (or any letter followed by a colon) in Windows is an absolute path.

For

p = Paths.get("/temp");
System.out.println(p.toAbsolutePath());

Anything that starts with / in Linux indicates that it's an absolute path. This one? I'm not too sure in Windows.

For

Path p = Paths.get("\\temp");
System.out.println(p.toAbsolutePath());

I think it will print something like c:\temp in Windows as c: is the default directory in Windows.

This is all I have for you, Paul.

Paul, I asked you something and you ask me to answer these questions. I'm sure there must be a reason you wanted me to answer these questions.

When you make a few comment(s) in response to what I've written here, would you mind kindly tie your reasoning behind asking me to answer these questions to the question in my original post? Many thanks, Paul.

Schmichael

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

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

Post by admin »

The reason I asked you to run the code is because such doubts are usually solved easily by running some code and observing the output.

All of the three print c:\temp
This tells you that on windows, all of these i.e. c:, /, and \\ indicate root.

Now, to answer your question, "what do you mean by root". By root, it means the root of the file system. For example, c: or d: on windows or / on Unix.
What intrigues me is that how Java compiler figures out when the root in Windows, i.e. c: and the root in Linux, i.e. / are present and used in the same program. How does Java compiler resolve this issue in cases that involve resolveSibling() and relativize() methods?
This tells me that you are still shaky on the basic concepts. Java compiler has nothing to do with this. It doesn't execute anything. It doesn't care about what paths are you using to resolve or relativize.
It is the JVM that does this and a JVM is always system dependent. So depending on which OS you are running your code on, it will behave accordingly.
The file system is OS dependent. JVM cannot do anything about it. It can only work with whatever the underlying system supports.


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

schchen2000
Posts: 106
Joined: Mon Mar 28, 2016 11:36 pm
Contact:

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

Post by schchen2000 »

I did not know / and \\ mean root in Windows. Thanks for sharing, Paul.

Schmichael

Post Reply

Who is online

Users browsing this forum: No registered users and 241 guests