Skip to content

Files

Latest commit

 

History

History
104 lines (41 loc) · 6.19 KB

core-java-volume-i—fundamentals--eleventh-edition.md

File metadata and controls

104 lines (41 loc) · 6.19 KB

Core Java Volume I—Fundamentals, Eleventh Edition

> Home

Chapter 6: Interfaces, Lambda Expressions, and Inner Classes

One of the interfaces, BiFunction<T, U, R>, describes functions with parameter types T and U and return type R. You can save our string comparison lambda in a variable of that type: (link)

Vikram: [You can think of it as takes any two parameters and returns one.]

You never specify the result type of a lambda expression. It is always inferred from context. For example, the expression (link)

A lambda expression is a block of code that you can pass around so it can be executed later, once or multiple times (link)

The rule is that any captured variable in a lambda expression must be effectively final. (link)

a lambda expression, you can only reference variables whose value doesn’t change (link)

The technical term for a block of code together with the values of the free variables is a closure. (link)

6.2.6 Variable Scope Often, you want to be able to access variables from an enclosing method or class in a lambda expression. Consider this example: (link)

Vikram: [Continue from here 28/09/2021]

Note that a lambda expression can only be rewritten as a method reference if the body of the lambda expression calls a single method and doesn’t do anything else (link)

In the second variant, the first parameter becomes the implicit parameter of the method. For example, String::compareToIgnoreCase is the same as (x, y) -> x.compareToIgnoreCase(y). (link)

Arrays.sort(strings, String::compareToIgnoreCase) (link)

The expression System.out::println is a method reference. It directs the compiler to produce an instance of a functional interface, overriding the single abstract method of the interface to call the given method. In this example, an ActionListener is produced whose actionPerformed(ActionEvent e) method calls System.out.println(e (link)

The Java API defines a number of very generic functional interfaces in the java.util.function package (link)

It is best to think of a lambda expression as a function, not an object, and to accept that it can be passed to a functional interface (link)

You can supply a lambda expression whenever an object of an interface with a single abstract method is expected. Such an interface is called a functional interface. (link)

The key point is that the actionPerformed method contains code that you want to execute later (link)

What happens if the exact same method is defined as a default method in one interface and then again as a method of a superclass or another interface (link)

As of Java 8, you are allowed to add static methods to interfaces (link)

If there is a common algorithm for comparing subclass objects, simply provide a single compareTo method in the superclass and declare it as final. (link)

Chapter 6Interfaces, Lambda Expressions, and Inner Classes (link)

Vikram: [Chapter 6 notes]

Chapter 12: Concurrency

var contents = new String(Files.readAllBytes( Path.of("alice.txt")), StandardCharsets.UTF_8); // read file into string String[] words = contents.split("[\P{L}]+"); // split along nonletters Arrays.parallelSort(words); (link)

Vikram: [Read file contents into a string in one line]

Chapter 8: Generic Programming

1 The Advantage of Type Paramete (link)

Vikram: [1) Otherwise, Object has to be used and a cast becomes compulsory. 2) Compile time checking. If objects do not match the parameter type, they are not accepted. ]

Generic classes and methods have type parameters. This allows them to describe precisely what should happen when they are instantiated with specific types. Prior to generic classes, programmers had to use the Object for writing code that works with multiple types. (link)

Vikram: [Defined Generic Classes]

> Home