
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 9150 Articles for Object Oriented Programming

3K+ Views
A lambda expression passed in a method that has an argument of type of functional interface. If we need to pass a lambda expression as an argument, the type of parameter receiving the lambda expression argument must be of a functional interface type.In the below example, the lambda expression can be passed in a method which argument's type is "TestInterface". Exampleinterface TestInterface { boolean test(int a); } class Test { // lambda expression can be passed as first argument in the check() method static boolean check(TestInterface ti, int b) { return ti.test(b); } } public class ... Read More

1K+ Views
The lambda expressions consist of two parts, one is parameter and another is an expression and these two parts have separated by an arrow (->) symbol. A lambda expression can access a variable of it's enclosing scope. A Lambda expression has access to both instance and static variables of it's enclosing class and also it can access local variables which are effectively final or final.Syntax( argument-list ) -> expressionExampleinterface TestInterface { void print(); } public class LambdaExpressionTest { int a; // instance variable static int b; // static variable LambdaExpressionTest(int x) { // constructor to initialise instance variable this.a = ... Read More

2K+ Views
Java supports lambda expressions but not the Closures. A lambda expression is an anonymous function and can be defined as a parameter. The Closures are like code fragments or code blocks that can be used without being a method or a class. It means that Closures can access variables not defined in its parameter list and also assign it to a variable.Syntax([comma seperated parameter-list]) -> {body}In the below example, the create() method has a local variable "value" with a short life and disappears when we exit the create() method. This method returns the closure to the caller in the main() method after that ... Read More

1K+ Views
The lambda expressions were introduced in Java 8 and facilitate functional programming. A lambda expression works nicely together only with functional interfaces and we cannot use lambda expressions with more than one abstract method.Characteristics of Lambda ExpressionOptional Type Declaration − There is no need to declare the type of a parameter. The compiler inferences the same from the value of the parameter.Optional Parenthesis around Parameter − There is no need to declare a single parameter in parenthesis. For multiple parameters, parentheses are required.Optional Curly Braces − There is no need to use curly braces in the expression body if the body contains a ... Read More

3K+ Views
A lambda expression body can't throw any exceptions that haven't specified in a functional interface. If the lambda expression can throw an exception then the "throws" clause of a functional interface must declare the same exception or one of its subtype.Exampleinterface Student { void studentData(String name) throws Exception; } public class LambdaExceptionTest { public static void main(String[] args) { // lamba expression Student student = name -> { System.out.println("The Student name is: " + name); throw new Exception(); }; try { student.studentData("Adithya"); } catch(Exception e) { } } }OutputThe Student name is: Adithya

2K+ Views
The effectively final variables refer to local variables that are not declared final explicitly and can't be changed once initialized. A lambda expression can use a local variable in outer scopes only if they are effectively final.Syntax(optional) (Arguments) -> bodyIn the below example, the "size" variable is not declared as final but it's effective final because we are not modifying the value of the "size" variable.Exampleinterface Employee { void empData(String empName); } public class LambdaEffectivelyFinalTest { public static void main(String[] args) { int size = 100; Employee emp = name -> { // lambda ... Read More

3K+ Views
A lambda expression is an inline code that implements a functional interface without creating a concrete or anonymous class. A lambda expression is basically an anonymous method.Advantages of Lambda ExpressionFewer Lines of Code − One of the most benefits of a lambda expression is to reduce the amount of code. We know that lambda expressions can be used only with a functional interface. For instance, Runnable is a functional interface, so we can easily apply lambda expressions.Sequential and Parallel execution support by passing behavior as an argument in methods − By using Stream API in Java 8, the functions are passed to collection methods. Now ... Read More

315 Views
A lambda expression is an anonymous function without having any name and does not belong to any class that means it is a block of code that can be passed around to execute.Syntax(parameter-list) -> {body}We can implement a lambda expression without creating an anonymous inner class in the below program. For the button's ActionListener interface, we need to override one abstract method addActionListener() and implement the block of code using the lambda expression.Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; public class LambdaExpressionButtonTest extends JFrame { private JButton btn; public LambdaExpressionButtonTest() { btn = new JButton("Click on the button"); ... Read More

3K+ Views
Anonymous class is an inner class without a name, which means that we can declare and instantiate class at the same time. A lambda expression is a short form for writing an anonymous class. By using a lambda expression, we can declare methods without any name.Anonymous class vs Lambda ExpressionAn anonymous class object generates a separate class file after compilation that increases the size of a jar file while a lambda expression is converted into a private method. It uses invokedynamic bytecode instruction to bind this method dynamically, which saves time and memory.We use this keyword to represent the current class in lambda expression while in the ... Read More

7K+ Views
The lambda expressions are introduced in Java 8. It is one of the most popular features of Java 8 and brings functional programming capabilities to Java. By using a lambda expression, we can directly write the implementation for a method in Java.In the below program, we can create a thread by implementing the Runnable interface using lamda expression. While using the lambda expressions, we can skip the new Runnable() and run() method because the compiler knows that Thread object takes a Runnable object and that contains only one method run() that takes no argument.Examplepublic class LambdaThreadTest { public static void main(String args[]) { ... Read More