Henry Wong wrote:
Please explain what you mean by "mixed up"..... and also... Keep in mind that there are two threads here. The one thread in the single thread executor, and the main thread which is running the code in the main() method.
João Victor Gomes wrote:
Taking the following code separately:
It means that it will initialize all the instance elements and constructor in order to finish this static initializer, so the other static elements can be initialized as well. Is that right?
Could anyone help me with this one?
Jesper de Jong wrote:
Florian Jedamzik wrote:What is going on here is:
in the Function "func2" the Method that acts up First, is compose(),
after it comes "andThen(). And last but not least func gets to
run, am I right here?
This is what happens:
In the following line:
1. The method 'andThen' is called on 'func' with a lambda expression as its argument. This returns a value, which is another Function object.
2. The method 'compose' is called on the Function that was returned in step 1. This returns another Function object.
When you then call func2.apply("4") then the following happens:
3. You are calling 'apply' on the Function object that was returned by the 'compose' method in step 2. This method first executes the lambda expression that was passed to it, which is in this case: (String s) -> Integer.parseInt(s) - so the "4" that you pass to apply() is converted to an int.
4. The 'compose' method then calls 'apply' on the Function object that it was called on in step 2 - so it calls 'apply' with the output of the lambda (the integer 4) on the Function object that was returned in step 1, which was created by the 'andThen' method.
5. The Function object returned by the 'andThen' method does this: it first calls the 'apply' of the Function object on which it was called itself, and then it calls the lambda expression that you passed to it on the result of the first 'apply' (so, this is exactly the opposite order as what 'compose' does). So: func.apply() is called with the integer 4. This translates the integer 4 back into a string "4". And then the lambda (String s) -> s+"2" is called, which concatenates strings "4" and "2", returning "42".
Stephan van Hulst wrote:That's incorrect. andThen() is performed first, and then compose() is performed, because compose() is to the right in the method chain.
First you have func, which is a Integer -> String. Then you perform andThen(), which turns it into another Integer -> String. Finally you perform compose(), which turns it into a String -> String.
Why would compose() be executed first?