0

I was trying to understand what's going on in this code. I am sure it referred to Java 8's method reference but I didnt get the idea.

I need to understand the last line:

...
subscribe(result::setResult,result::setErrorResult);

What actually happens behind the scene? what subscribe actually gets and how would you implement it without lambad's (to understand this idea)

taken from here:

public DeferredResult<FlightDetails> getAllFlightDetails() {   Observable<String>availableFlightBookings=flightBookingIntegrationService.getAvailableFlightBookings();
        Observable<String> couponId=couponIntegrationService.getCoupon();

        DeferredResult<FlightDetails> result = new DeferredResult();

        Observable.zip(availableFlightBookings,couponId, (avaliable, coupon) -> {
         // do some logic here or just..
        return new FlightDetails(avaliable,coupon);
        }).subscribe(result::setResult,result::setErrorResult);
        return result;
    }

subscribe signature:

  public final Subscription subscribe(final Action1<? super T> onNext, final Action1<Throwable> onError)

Thank you, ray.

rayman
  • 20,786
  • 45
  • 148
  • 246
  • @SotiriosDelimanolis The OP knows it's a method reference. I don't think that link answers the questions "what goes on behind the scenes?" and "how would you implement it without lambdas?". – Paul Boddington Aug 03 '15 at 16:30
  • @pbabcdefp You misunderstand what a duplicate is. A duplicate isn't a duplicate _question_. A duplicate is a post that contains an answer to this question. The accepted answer (and the other answers) show an alternative with lambdas and without them. It also explains what is passed as an argument and what happens behind the scenes. – Sotirios Delimanolis Aug 03 '15 at 16:32
  • @Sotirios Delimanolis, pbabcdefp is right. I didnt ask about the double colon or the method reference. i asked about this specific case and how you would do it without lambda's to make it clear for me. – rayman Aug 03 '15 at 16:45
  • @SotiriosDelimanolis I don't agree.The accepted answer says that an alternative to `reduce(Math::max);` is `reduce(new IntBinaryOperator() {...`. While this is true, it suggests that a method reference is just syntactic sugar for creating a new object using an anonymous class. One thing about method references is that they do not always create a new object. None of that behind the scenes stuff is explained in that other post, but nobody can explain it here because the question has been closed. – Paul Boddington Aug 03 '15 at 16:48
  • We don't need a new question for each different use of a method reference for each applicable method/functional-interface combo. – Sotirios Delimanolis Aug 03 '15 at 17:05
  • There are tons of resources about the behind the scenes: http://stackoverflow.com/questions/21858482/what-is-a-java-8-lambda-expression-compiled-to?lq=1, http://stackoverflow.com/questions/16827262/how-will-java-lambda-functions-be-compiled, http://stackoverflow.com/questions/26257266/are-java-8-lambdas-compiled-as-inner-classes-methods-or-something-else?lq=1, http://stackoverflow.com/questions/23983832/is-method-reference-caching-a-good-idea-in-java-8?lq=1 – Sotirios Delimanolis Aug 03 '15 at 17:06
  • Here's some more: http://stackoverflow.com/questions/29674645/when-does-jvm-decide-to-reuse-old-lambda?lq=1, http://stackoverflow.com/questions/27524445/does-a-lambda-expression-create-an-object-on-the-heap-every-time-its-executed?lq=1 – Sotirios Delimanolis Aug 03 '15 at 17:07
  • @SotiriosDelimanolis I agree it's a duplicate. My point is that if you're going to close a question so quickly, please choose a duplicate that's actually relevant. – Paul Boddington Aug 03 '15 at 17:09
  • @pbabcdefp I see _what subscribe actually gets and how would you implement it without lambad's (to understand this idea)_ The accepted answer is the duplicate post states _Although it produces different bytecodes for all three code snippets, they are semantically equal, so the last two can be considered to be short (and probably more efficient) versions of the IntBinaryOperator implementation above!_ That answers the question in my opinion. The code snippet with the anonymous class answers the _without lambda_. – Sotirios Delimanolis Aug 03 '15 at 17:12
  • It don't think we're going to agree. To me, an answer would explain the rules for exactly how standard compilers interpret method references (giving equivalent code using Java 7) not just say that the bytecode produced can vary. Only one of the 3 code snippets in that answer is Java 7 code, and it isn't equivalent to a method reference (because it involves the `new` keyword), so it can't possibly answer the question here. – Paul Boddington Aug 03 '15 at 17:37
  • 1
    They don't literally mean equivalent. Only a method reference could generate bytecode for a method reference. IMO, they mean "How could you achieve the same behavior without the method reference?". I don't believe OP is looking for a bytecode analysis and explanation from the JLS and JVMS. If you want to explain all that in so much detail, go do it in one of the linked posts. There's no reason to provide questions and answers all over the place, making it so much harder to find. – Sotirios Delimanolis Aug 03 '15 at 17:47

0 Answers0