• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

NIO getNameCount()

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guys i have a doubt in the below question:

Assuming the current directory is /seals/harp/food, what is the result of executing the
following code?

A. 0
B. 1
C. 2
D. 3
E. 4
F. The code throws a runtime exception because of line h1.

I understand that the getNAmeCount is 3 and the answer is d but in the book its mentioned as B. Can someone please help me understand!!
Thanks in advance!!

 
Saloon Keeper
Posts: 13369
295
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All answers are wrong.

The real answer is: "It depends on the Java implementation". The normalize() method has no fixed definition.

It's likely that getNameCount() will return 1, but keep in mind that it is not guaranteed.

Here's the explanation: Paths don't care what is on your hard drive or what directory you are currently in. Except for only a few methods, none of the methods on Path will access your file system. A path like Paths.get("/i/like/puppies") is really just a thin wrapper around the string "/i/like/puppies", and it doesn't care whether the names between the slashes refer to real directories. Maybe it will make it easier for you to think of a path as if it was really just a string, and the methods on Path are really just string operations.

So, the method Path.normalize() is really just a string operation, and it doesn't look at your current working directory. In most cases (but not all!), this operation will just remove the name "." from your path, and it will remove ".." and the preceding directory name from your path. So, for the path Paths.get("."), it will just remove the ".", and leave you with the empty string.

This means that on most Java implementations, Paths.get(".").normalize() returns the same path as Paths.get(""). Paths.get("") has one path element (the empty string), so its name count is 1.

What book are you using? This particular question seems of low quality, because not only is the answer they give wrong, the code is poorly written as well. This whole piece:

Can be replaced with just:
 
Marshal
Posts: 74345
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe the loop was added just to confuse readers.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic