title: Java 8: How BiFunction works while using andThen() and apply() methods
body: <p>Currently <code>BiFunction</code> interface has two methods <code>andThen()</code> and <code>apply()</code>. I found different examples in web, but the below example is something I can't figure out - 'How the chaining of these methods works'</p>
  
  <p><strong>Example 1:</strong></p>
  
  <pre><code>BiFunction&lt;String, String,String&gt; bi = (x, y) -&gt; {
                      return x + y;
                      };
  
  Function&lt;String,String&gt; f = x-&gt; x+" spinner";
  
  System.out.println(bi.andThen(f).apply("hello", " world"));
  </code></pre>
  
  <p>After reading following link '<a href="https://stackoverflow.com/questions/50074620/default-andthen-method-in-bifunction-interface">Default andThen() method in BiFunction interface</a>', I took below lines </p>
  
  <blockquote>
    <p>Assume we had two functions f and g , function f doing some logic and function g doing some other type of logic so when you compose f.andThen(g) that essentially means g(f(x)) i.e. we first apply the function given as argument f(x) and then apply the function g to the result.</p>
  </blockquote>
  
  <p>and concluded that <code>bi.apply("hello", " world"))</code> happens first and then the <code>result</code> is
  passed on to <code>bi.andThen(f(result))</code> and this gives final output <code>hello world spinner</code>.</p>
  
  <p>I understood to some extent, but not completely satisfied. I had to ask this because in the past I have used builder pattern and it is something like the below,</p>
  
  <pre><code>BankAccount account = new BankAccount.Builder("bank_account")
                                   .withOwner("account_owner")
                                   .atBranch("branch_name")
                                   .openingBalance(balance)
                                   .build();
  </code></pre>
  
  <p>Here, the method calling happens in sequence, first the <code>Builder</code> (static) class gets initialised, then <code>withOwner</code> assigns owner name and returns builder, and then branch name is assigned and returns builder,
  next opening balance is given and returns builder, and finally build will return <code>BankAccount</code> instance. Please see the <code>BankAccount</code> class.</p>
  
  <pre><code>public class BankAccount {
  
      public static class Builder {
  
          private long accountNumber; 
          private String owner;
          private String branch;
          private double balance;
  
          public Builder(long accountNumber) {
              this.accountNumber = accountNumber;
          }
  
          public Builder withOwner(String owner){
              this.owner = owner;
              return this; 
          }
  
          public Builder atBranch(String branch){
              this.branch = branch;
              return this;
          }
  
          public Builder openingBalance(double balance){
              this.balance = balance;
              return this;
          }
  
  
  
          public BankAccount build(){
              //Here we create the actual bank account object, which is always in a fully initialised state when it's returned.
  
              BankAccount account = new BankAccount();  //Since the builder is in the BankAccount class, we can invoke its private constructor.
              account.accountNumber = this.accountNumber;
              account.owner = this.owner;
              account.branch = this.branch;
              account.balance = this.balance;
  
              return account;
          }
      }
  
  }
  </code></pre>
  
  <p>As you can see the methods are called in sequence and the output of methods -<code>withOwner</code>, <code>atBranch</code>, <code>openingBalance</code>, is <code>Builder</code> instance in the chain. and for me this is called chaining of methods, because the output of each method in sequence is used for later and this is very clear. But, my ask is - how the chaining of methods of the above example 1 <code>(BiFunction and its methods chaining)</code> works <strong>internally</strong>.</p>