Java Reference
In-Depth Information
As you can see by looking at this code, embedding the lambda that replaces spaces
with hyphens in the call to changeStr( ) is both convenient and easy to understand.
This is because it is a short, expression lambda that simply calls replace( ) to replace
spaces with hyphens. The replace( ) method is another method defined by the String
class. The version used here takes as arguments the character to be replaced and its
replacement. It returns a modified string.
For the sake of illustration, the lambda that inverts the case of the letters in a string
is also embedded in the call to changeStr( ) . However, in this case, rather unwieldy
code is produced that is somewhat hard to follow. Usually, it is better to assign such a
lambda to a separate reference variable (as was done for the string-reversing
lambda), and then pass that variable to the method. Of course, it is technically correct
to pass a block lambda as an argument, as the example shows.
One other point: notice that the invert-case lambda uses the static methods isUp-
perCase( ) , toUpperCase( ) , and toLowerCase( ) defined by Character . Recall that
Character is a wrapper class for char . The isUpperCase( ) method returns true if
its argument is an uppercase letter and false otherwise. The toUpperCase( ) and
Search WWH ::




Custom Search