Java Reference
In-Depth Information
The null reference
The null keyword is a special literal value that is a reference to nothing, or an
absence of a reference. The null value is unique because it is a member of every
reference type. You can assign null to variables of any reference type. For example:
String s = null ;
Point p = null ;
Lambda Expressions
In Java 8, a major new feature was introduced— lambda expressions . These are a very
common programming language construct, and in particular are extremely widely
used in the family of languages known as functional programming languages (e.g.,
Lisp, Haskell, and OCaml). The power and flexibility of lambdas goes far beyond
just functional languages, and they can be found in almost all modern program‐
ming languages.
Deinition of a Lambda Expression
A lambda expression is essentially a function that does not have a name, and can be
treated as a value in the language. As Java does not allow code to run around on its
own outside of classes, in Java, this means that a lambda is an anonymous method
that is defined on some class (that is possibly unknown to the developer).
The syntax for a lambda expression looks like this:
( paramlist ) -> { statements }
One simple, very traditional example:
Runnable r = () -> System . out . println ( "Hello World" );
When a lambda expression is used as a value it is automatically converted to a new
object of the correct type for the variable that it is being placed into. This auto-
conversion and type inference is essential to Java's approach to lambda expressions.
Unfortunately, it relies on a proper understanding of Java's type system as a whole.
“Lambda Expressions” on page 171 provides a more detailed explanation of lambda
expressions—so for now, it suffices to simply recognize the syntax for lambdas.
A slightly more complex example:
ActionListener listener = ( e ) -> {
System . out . println ( "Event fired at: " + e . getWhen ());
System . out . println ( "Event command: " + e . getActionCommand ());
};
 
Search WWH ::




Custom Search