Reputation: 3770
In java there often objects that serve only to wrap a function and don't have any state of their own. Example:
class Foo {
void foo () {
System.out.println("foo");
}
static public void main(String[] arg) {
Foo foo = new Foo();
foo.foo();
}
}
I'd like to know if the expression new Foo()
is optimized to what in C++ would be an assignment of function pointer. It seems like an obvious thing to do, but when I think about it, the compiler would have to check that foo
is not used for synchronization (and possibly something else?). Does the standard say anything about this?
Upvotes: 2
Views: 3541
Reputation: 49744
The standard (the VM spec and the JLS that is) doesn't say anything about this, it's entirely up to the VM implementations to deal with optimisations.
All the standards specify is a set of invariants that need to be observed. If the VM guarantees that what you do will look like creating an object, then calling a method, then disposing of it, it can do what it wants.
Although the exact manner of optimisation can vary, it is extremely rare that things on this low level are a bottleneck. But as optimisations are done runtime, you can be reasonably certain that they will only be performed if your code is invoked a lot; code that is infrequently used is interpreted rather than compiled into machine code. (But even this may change from VM to VM and may depend on specific command line options.) In your case what would probably happen is that after a lot of runs the VM inlines the method completely, getting rid of both the object creation and the method invocation.
The one thing you can definitely do to "optimise" your code is to declare Foo
(and foo()
) final.
Upvotes: 1
Reputation: 1973
Not sure what the standard says. But I don't think any optimizations like you mentioned are done. Why do I think I'm sure? Because it would break java.lang.Class functionality. From API doc:
Instances of the class Class represent classes and interfaces in a running Java application.
Upvotes: 0
Reputation: 35011
new Foo() is going to create a new object
In Java, there are Objects, arrays, and primitives, and nothing else
Upvotes: 0
Reputation: 4297
Imagine a scenario when you have loads of static helper methods. If you implement them all in the class where you want to use them, your class will get bigger and bigger.
However if you write them in a separate singleton, or in a static class, then you only have to hold one reference, and call the methods through that, so you can save up memory when you work with multiple instances.
I might be wrong, but this is the first thing that came to mind.
Upvotes: 0
Reputation: 133577
If you don't have any state then you could just make these method static
. I don't think that the java compiler would optimize it as you think even because that would alter the semantics. If an object is created through new
then turning it into something else would alter the semantics.
I don't think the concept of assignment of function pointer is used at all.
Upvotes: 0
Reputation: 6153
It is said, that the creation of an object takes only ~ 10 instructions. So it is a very very cheap operation.
from here
Sun estimates allocation costs at approximately ten machine instructions.
So there is almost no overhead in creating a new object. If you made everything static and put it in only one class, this might get cluttered very soon.
Upvotes: 0