Language 3 of 10 · Topic 0.7

Methods, Pass-by-Value Semantics & Lambdas / Functional Interfaces

1Concept

Java is strictly Pass-by-Value for everything. When passing an object, the method receives a copy of the reference pointer. Functional Interfaces (@FunctionalInterface like Function, Predicate, Consumer) enable Lambdas ((params) -> expression) and Method References (Class::method).

2Architecture Diagram

Pass-by-Value in Java:
 Caller: [ objRef (Points to Heap Addr 0x500) ]
 Callee: [ copyRef = 0x500 ] (Points to same heap object, but reassigning copyRef = null does not affect caller!)

3Code Example

Stage 0 Language Foundations
import java.util.List;
import java.util.function.Predicate;

public class LambdaDemo {
    @FunctionalInterface
    interface MathOperation {
        int compute(int a, int b);
    }

    public static void main(String[] args) {
        MathOperation add = (a, b) -> a + b;
        MathOperation multiply = (a, b) -> a * b;

        System.out.println("Add 15 + 25 = " + add.compute(15, 25));
        System.out.println("Multiply 6 * 7 = " + multiply.compute(6, 7));

        var names = List.of("Alice", "Bob", "Charlie", "David");
        Predicate<String> startsWithC = name -> name.startsWith("C");

        names.stream()
             .filter(startsWithC)
             .forEach(System.out::println); // Method reference
    }
}

4Expected Output

Add 15 + 25 = 40
Multiply 6 * 7 = 42
Charlie

5Key Takeaways

  • Java never passes objects by reference; it passes copies of reference pointers.
  • Functional interfaces contain exactly one abstract method (Single Abstract Method - SAM).
  • Lambdas can capture effectively final local variables from their enclosing scope.