2

I made this method:

public void update(){
        for(GameObject object : objects)
            object.update();
}

And then my IDE I could use for each instead any, that I what thought I did, any I let it replace it. This it what it gave me:

public void update(){
        objects.forEach(GameObject::update);
}

At first glance, this seems to better, and then my IDE was saying you can convert this to lambda expressions which would be:

public void update(IUpdater updater){
        objects.forEach((object) -> object.update());
}

Which gave the impression that GameObject:: update is some time of lambda expressions, bare in mind the only new stuff to java 8 that I know of (object) -> object.update() replaces anonymous class guess it still under the hood somewhere.

Does this essence 'objects.forEach(GameObject::update)' means:

public void update(){
    objects.forEach(new Consumer<GameObject>() {
        @Override
        public void accept(GameObject object) {
            object.update();
        }
    });
}

And if that is the case then would my original method be more efficient that GameObject::update. Have left it the method as GameObject::update but will change it back if find original method be more efficient.

I am asking the question on a purely on the basis of performance.

  • 2
    `GameObject::update` is a [method reference](https://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html) – flakes Sep 09 '16 at 13:52
  • The performance difference is neglible. – marstran Sep 09 '16 at 13:54
  • 2
    "I am asking question on a purely on performance bases." You should never ever EVER microoptimize on such a level before it has been proven that you do in fact have performance issues at that point. [Premature optimization is the devil.](https://en.wikipedia.org/wiki/Program_optimization#When_to_optimize) – Jan Dörrenhaus Sep 09 '16 at 13:55
  • 1
    Don’t think just because an IDE says that you *can* do something, it is automatically better. Usually, if you have a lambda expression, they say, you *can* convert it to a method reference and when you have a method reference, they say, you *can* convert it to a lambda expression, and in both cases they say you *can* convert it to an anonymous class and vice versa… – Holger Sep 09 '16 at 17:36

1 Answers1

2

I would use which ever one you feel is simplest and clearest and this likely to perform well enough. Once you have profiled your application and shown this is one of your biggest problems, then you should consider optimising it.

However for academic purposes...

One key difference between the method reference and the lambda expression is that these are non-capturing. This means the object is only ever created once on HotSpot Java 8. If you use an anonymous inner class it will be created every time the code is called (but not on each iteration) Note: using a for-each loop uses an Iterator and this might create a new object is escape analysis doesn't eliminate it.

Either way the difference is likely to be far less than a micro-second.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • 2
    *When* a method reference or lambda expression is non-capturing, there will be only one instance (implementation dependent). And this is not a “difference between the method reference and the lambda expression”, I guess that’s not what you wanted to say… – Holger Sep 09 '16 at 17:34
  • 1
    The lambda in question here (`object -> object.update()`) is non-capturing too. – Brian Goetz Sep 09 '16 at 19:29