1)I was looking at the source code of the Arrays.copyOf and seems like it uses System.arrayCopy.
but this is what is declared in System.arrayCopy -
What is happening here? I do understand that Java is relying on the native calls to copy teh array but jsut interested in details.
2 ) the other question is there are 2 ways of deleting the file - example if you want to remove a file, you do it by calling file.delete() or Runtime.getRuntime().exec("rm filename"); performance wise which is better? I was told that using file io is better but I dont understand how.
About 2: Even if there is a very small performance gain I would never use your second method. You're depended on the OS, need to read the streams of the process and I can probably think of another 200 reasons why this is a bad idea.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
There are two ways of getting to the other side of the street. You could walk across the street. Or you could build a springboard on one side and install a net on the other side and hire a few people to make sure that when you launched yourself from the springboard, the net would actually catch you.
Hari priya wrote:What is happening here? I do understand that Java is relying on the native calls to copy teh array but jsut interested in details.
I can't be sure without looking into the JVM source code, but my guess is that the JVM is using some form of low-level memory copying that you can't do in Java. This will make the call faster.
Hari priya wrote:BTW - is it true that there will be better performance using File I/O and NOT with using System calls? I never understood how...
A pure system call (as in a native method) will most likely be the most efficient. However, that's probably what File.delete() already uses. Using a system call by using Runtime.exec() first needs to create a new process, then read the results from its streams (or the process may hang). Both are quite inefficient compared to calling a single method that already uses native calls.
For your information, I checked the source code of File.delete() (available in file src.zip inside your JDK root folder). It calls fs.delete(this), with fs being an instance of a sub class of non-public class FileSystem. For Windows the implementation of this method uses a private native method. So my assumption that File.delete() uses a native call is correct, at least on Windows.
Hari priya wrote:...example if you want to remove a file, you do it by calling file.delete() or Runtime.getRuntime().exec("rm filename"); performance wise which is better?
To be honest, I wouldn't spend too much time worrying about performance; at least at this stage. Far better is to think about clarity; and what would you prefer to see:
Runtime.getRuntime().exec("rm " + file.getAbsolutePath()); (which is in fact what you would need)?
What are the chances of making a mistake with the first and the second?
Which is easier for the poor soul that has to debug your program after you're gone?
Read the quote at the bottom of Wouter's post; it's very good. You might also look at mine. And if you're still not convinced, have a look at this. I don't always agree with Brian Goetz, but he's an industry guru for a reason; and in this case, he's spot on.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
The only thing that kept the leeches off of me was this tiny ad: