1

I am new to functional programming, so far i have understood how to use it, anonymous function etc.

I saw many examples of code where the object needed as parameter in my lambda expression actually doesn't exist in that moment (it isn't instantiated).

For example, is this:

myClass.myMethod(c -> {my overridden code});

the same as this

myClass.myMethod(new String() -> {my overridden code});

considering that c is not declared in my code and myMethod correctly implements a functional interface which abstract method requires a String?

EDIT:

I got some problems with this question: JavaFX ComboBox Image With this part of code:

comboBox.setCellFactory(c -> new StatusListCell());

I can't figure out where c is taken from, it's not declared at all, that's why i was wondering if lambda expressions could create new objects automatically.

Talenel
  • 422
  • 2
  • 6
  • 25
Mattia Surricchio
  • 1,362
  • 2
  • 21
  • 49
  • Sorry, really not sure what you are asking. The argument comes from wherever the lambda is called. So `myMethod` seems to take a `Consumer` or some such; at some later point something will call `accept` on that `Consumer` and provide a `String`; this then becomes the argument. – Boris the Spider Jun 10 '18 at 22:14
  • 1
    A Lambda expression in Java is a short hand for the full java code providing an implementation of the interface in question. The names on the left side of the arrow are just placeholders, not full code. Note that a lambda expression is a _definition_ not an invocation. – Thorbjørn Ravn Andersen Jun 10 '18 at 22:37
  • 1
    Re, "I was wondering if lamba expressions could create new objects automatically." Yes. Every time a lambda is evaluated, it creates a new instance of an anonymous inner class, exactly the way @YassinHajaj shows in his answer. – Solomon Slow Jun 11 '18 at 00:02

1 Answers1

2

c is actually only a placeholder, like a parameter in a method would be (which does not differ from the functioning of the lambda here).

myClass.myMethod(c -> {my overridden code});

is the equivalent of the following

myClass.myMethod(new Consumer<String>(){
    @Override
    public void accept(String c) {
        {my overridden code}
    }
}

So the answer to your question is : No. The lambda represents a method, a function but is not an executable piece by itself, it has to be invoked with outside parameters.

Yassin Hajaj
  • 21,337
  • 9
  • 51
  • 89
  • Got it, that's what i expected! I got some problems with this question: https://stackoverflow.com/questions/38030534/javafx-combobox-image This line: comboBox.setCellFactory(c -> new StatusListCell()); Where is c taken from? It should be a ListCell, but it's not declared! – Mattia Surricchio Jun 10 '18 at 22:15
  • 1
    @MattiaSurricchio You do not need to worry about the `c` in your case since the function does not use it and returns a new instance of another object.. It's not declared because it's used by JavaFX in the background when needed. Your function will be invoked with a certain value but always return a new instance of the other object... – Yassin Hajaj Jun 10 '18 at 22:20
  • 1
    Um, the answer to the question asked, "Does lamba expression automatically create new object?" is _yes_, just like you show in your example, `new Consumer...` – Solomon Slow Jun 10 '18 at 23:59
  • @jameslarge what makes you think that? It may _behave_ (somewhat) like an anonymous class but its scoping rules are completely different and the language spec does not require that a new object is created. A lambda is an `invokedynamic` call site. – Boris the Spider Jun 11 '18 at 06:27
  • 1
    @jameslarge that’s the question in the *title*, which has been answered in [Does a lambda expression create an object on the heap every time it's executed?](https://stackoverflow.com/q/27524445/2711488) with “no, not always”, but that isn’t what the OP has asked in the question’s body. I’d say, the title needs a rework, however, I don’t know how to put the OP’s actual question into a single phrase… – Holger Jun 11 '18 at 09:45
  • @Holger *Does lamba expression automatically create new object for its entry parameters?* – Yassin Hajaj Jun 11 '18 at 10:45
  • 1
    Edited as suggested – Mattia Surricchio Jun 11 '18 at 15:55