Java Programming for Android Developers For Dummies
Book image
Explore Book Buy On Amazon
Sometimes, if you open code in Android Studio's editor and hover the mouse over certain words, you get an interesting surprise. Android Studio tells you that you can replace the anonymous inner class with a lambda expression.

java-programming-for-android-developers-2e-lambda-expressions
You can create a lambda expression.

Okay. What's a lambda expression? For starters, lambda is a letter in the Greek alphabet, and the term lambda expression comes from papers written in the 1930s by mathematician Alonzo Church.

In 2013, Oracle released Java 8, adding lambda expressions to the Java language. And in 2016, Google made Java 8 features available to Android developers.

What is a lambda expression exactly? A lambda expression is a concise way of declaring an interface that contains only one method. For example, an anonymous OnClickListener could have only one method, namely, the onClick method. So you can replace this anonymous OnClickListener with a lambda expression.

If you respond to the message by pressing Alt+Enter, Android Studio offers you a Replace with Lambda option. If you accept this option, Android Studio turns your code into this stuff.

package com.allmycode.a11_05;

import android.os.Bundle;

import android.support.v7.app.AppCompatActivity;

import android.widget.Button;

import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

Button button;

TextView textView;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

button = (Button) findViewById(R.id.button);

button.setOnClickListener(<strong>view -> textView.setText(R.string.you_clicked)</strong>);

textView = (TextView) findViewById(R.id.textView);

}

}

This code uses a lambda expression. The image illustrates the transition from a class that implements a one-method interface to a lambda expression.

java-programming-for-android-developers-2e-interface-into-lambda
Turning an interface into a lambda expression.

Notice the lightweight role of the word view. When you declare an onClick method, you give the method a parameter of type View even if the statements inside the method don't use that parameter. In the same way, when you create a lambda expression for an onClick method, you preface the -> symbol with a parameter name, even if you don't use that parameter name to the left of the -> symbol.

In order to use lambda expressions, you must satisfy certain requirements. For example, you must compile your code with Java 8 or higher. Your Android Studio version must be 2.1 or higher. And your project's build.gradle file must include the following code:

android {

...

defaultConfig {

...

<strong> jackOptions {</strong>

<strong> enabled true</strong>

<strong> }</strong>

}

...

}

A lambda expression may have more than one parameter to the left of the -> symbol. If it does, you must enclose all the parameters in parentheses and separate the parameters from one another with commas. For example, the expression

(price1, price2) -> price1 + price2

is a valid lambda expression.

If you're comfortable with lambda expressions, you can make your code much more readable. What started out as about ten lines of code can easily become only part of a line.

About This Article

This article is from the book:

About the book author:

Barry Burd, PhD, is a professor in the Department of Mathematics and Computer Science at Drew University in Madison, New Jersey. He has lectured at conferences in the United States, Europe, Australia, and Asia. He hosts podcasts and videos about software and other technology topics. He is the author of many articles and books, including Java For Dummies.

This article can be found in the category: