Java Reference
In-Depth Information
Here,
inStr
refers to the string that will be acted on, and
outStr
will receive the mod-
ified string.
5
. Define a lambda expression that reverses the characters in a string and assign it to a
StringFunc
reference. Notice that this is another example of a block lambda.
6
. Call
changeStr( )
, passing in the
reverse
lambda and
inStr
. Assign the result to
out-
Str
, and display the result.
Because the first parameter to
changeStr( )
is of type
StringFunc
, the
reverse
lambda can be passed to it. Recall that a lambda expression causes an instance of its
target type to be created, which in this case is
StringFunc
. Thus, a lambda expres-
sion gives you a way to effectively pass a code sequence to a method.
7
. Finish the program by adding lambdas that replace spaces with hyphens and invert
the case of the letters, as shown next. Notice that both of these lambdas are embed-
ded in the call to
changeStr( )
, itself, rather than using a separate
StringFunc
vari-
able.