what is the costly operation on JVM ?
This is probably not a meaningful or quantifiable thing:
- You haven't said what an "operation" is.
- Depending on what you mean by "operation", the "cost" most likely depends on the context. For instance
while (i < 999) i++; probably has ZERO cost once the JIT compiler has optimized it.
I remember that my professor said that new() operation is the most costly operation on JVM. Is it true?
I mean, than opening a file?
Certainly not. The process of opening a file will create a number of objects.
But if you call opening a file an "operation", how small or big can an operation be?
and how can I know the cost of any operation in Java?
It is very difficult to write a benchmark that isolates and measures the cost of a given operation. And it is generally pointless too, because the JVM optimizes sequences of operations.
For example, the difference between ...
Even your example is difficult to measure. For example:
Part of the cost of those statements needs to be amortized with the cost of running the garbage collector.
The JIT compiler may decide that some or all of those statements don't effect the outcome of the computation, and optimize them away completely.