File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes a question of optimization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "a question of optimization" Watch "a question of optimization" New topic
Author

a question of optimization

Pablo Olmos
Greenhorn

Joined: Aug 20, 2004
Posts: 10
Hi everyone,

Recently a question came up with a coworker. What is faster and more robust to do?:



or



The question came up because the original code was:



It is unknown if the path string contains a trailing slash ("/"). So if a slash isn't concatenated between the path and file name, the created File object would not be a valid representation of the file.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Given that the proper directory separator is not necessarily a '/', the first one is simply wrong. There's a static variable in the File class that contains the right characters; but personally I'd use #2. Don't worry about efficiency at this micro-level -- worry about using efficient algorithms.


[Jess in Action][AskingGoodQuestions]
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
I agree with Ernest. #2 is more robust, and that trumps speed per default.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
 
subject: a question of optimization