Found 9150 Articles for Object Oriented Programming

Are lambda expressions objects in Java?

raja
Updated on 10-Jul-2020 11:04:59

2K+ Views

Yes, any lambda expression is an object in Java. It is an instance of a functional interface. We have assigned a lambda expression to any variable and pass it like any other object.Syntax(parameters) -> expression              or (parameters) -> { statements; }In the below example, how a lambda expression has assigned to a variable and how it can be invoked.Example@FunctionalInterface interface ComparatorTask {    public boolean compare(int t1, int t2); } public class LambdaObjectTest {    public static void main(String[] args) {       ComparatorTask ctask = (int t1, int t2) -> {return t1 ... Read More

How to write the comparator as a lambda expression in Java?

raja
Updated on 06-Dec-2019 10:26:25

5K+ Views

A lambda expression is an anonymous method and doesn't execute on its own in java. Instead, it is used to implement a method defined by the functional interface. A lambda expression used with any functional interface and Comparator is a functional interface. The Comparator interface has used when sorting a collection of objects compared with each other.In the below example, we can sort the employee list by name using the Comparator interface.Exampleimport java.util.ArrayList; import java.util.Collections; import java.util.List; class Employee {    int id;    String name;    double salary;    public Employee(int id, String name, double salary) {       super();   ... Read More

What are the scoping rules for lambda expressions in Java?

raja
Updated on 10-Jul-2020 08:51:15

553 Views

There are different scoping rules for lambda expression in Java. In lambda expressions, this and super keywords are lexically scoped means that this keyword refers to the object of the enclosing type and the super keyword refers to the enclosing superclass. In the case of an anonymous class, they are relative to the anonymous class itself. Similarly, local variables declared in lambda expression conflicts with variables declared in the enclosing class. In the case of an anonymous class, they are allowed to shadow variables in the enclosing class.Example@FunctionalInterface interface TestInterface {    int calculate(int x, int y); } class Test {    public ... Read More

How many parameters can a lambda expression have in Java?

raja
Updated on 10-Jul-2020 08:32:24

4K+ Views

The lambda expressions are easy and contain three parts like parameters (method arguments), arrow operator (->) and expressions (method body). The lambda expressions can be categorized into three types: no parameter lambda expressions, single parameter lambda expressions and multiple parameters lambda expressions.Lambda Expression with no parameterWe need to create no parameter lambda expression then start the expression with empty parenthesis.Syntax() -> {    //Body of no parameter lambda }Example(no parameter Lambda)import java.util.function.*; import java.util.Random; public class LambdaExpression1 {    public static void main(String args[]) {       NumberUtil num = new NumberUtil();       int randVal = num.getRandomValue(       ... Read More

What are block lambda expressions in Java?

raja
Updated on 10-Jul-2020 06:43:27

1K+ Views

A lambda block states that lambda expression with multiple statements. It expands the type of operations to perform with a lambda expression. The multiple statements containing bodies are called expression bodies. A lambda expression with expression bodies is called expression lambdas. Whenever we are using expression lambdas, explicitly use a return statement to return a value.Exampleinterface NumberFinder {    int finder(int number1, int number2); } public class LambdaNumberFinder {    public static void main(String args[]) {       NumberFinder numberFinder = (number1, number2) -> {          int temp = 0;          if(number1 > number2) ... Read More

How can we use lambda expressions with functional interfaces in Java?

raja
Updated on 10-Jul-2020 06:24:13

816 Views

The lambda expressions are anonymous functions and don't have any return type, access modifier and not belonging to any class. It can be used to simplify the implementation of the abstract method in a functional interface. Whenever there is a functional interface, we can use lambda expressions instead of anonymous inner classes.Syntax([comma seperated argument-list]) -> {body}Example@FunctionalInterface interface BonusCalculator {    public double calcBonus(int amount); } class EmpDetails {    public void getBonus(BonusCalculator calculator, int amount) {       double bonus = calculator.calcBonus(amount);       System.out.println("Bonus: " + bonus);    } } public class LambdaExpressionTest {    public static void main(String[] ... Read More

What is the data type of a lambda expression in Java?

raja
Updated on 02-Dec-2019 09:14:51

1K+ Views

The lambda expressions have a very simple, precise syntax and provide flexibility to specify the datatypes for the function parameters. Its return type is a parameter -> expression body to understand the syntax, we can divide it into three parts.Parameters : These are function method parameters and match with the signature of a function defined in the functional interface. Defining the data-type of parameters is optional but the number of parameters can match with the defined signatures in the interface.Expression Body : This is either a single statement or collection of statements that represent the function definition. Defining the data-type for return ... Read More

Difference between res.send and res.json in Express.js

Ayush Gupta
Updated on 02-Dec-2019 07:00:51

2K+ Views

Whenever an Express application server receives an HTTP request, it will provide the developer with an object, commonly referred to as res. For example, Exampleapp.get('/test', (req, res) => {    // use req and res here })The res object basically refers to the response that'll be sent out as part of this API call.The res.send function sets the content type to text/Html which means that the client will now treat it as text. It then returns the response to the client.The res.json function on the other handsets the content-type header to application/JSON so that the client treats the response string ... Read More

How to prevent moment.js from loading locales with webpack?

Ayush Gupta
Updated on 02-Dec-2019 06:57:27

452 Views

A local file is a .json file that contains a set of translations for the text strings used in a theme template file. A separate local file is used for every language.When you require moment.js in your code and pack it with webpack, the bundle size becomes huge because it includes all locale files.You can remove all locale files using the IgnorePlugin. For example, Exampleconst webpack = require('webpack'); module.exports = {    plugins: [       // Ignore all locale files of moment.js       new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),    ], }; // load specific locales in your code. ... Read More

Jasmine.js comparing arrays

Ayush Gupta
Updated on 02-Dec-2019 06:55:34

1K+ Views

Arrays can be compared in 2 ways −They refer to the same array object in memory.They may refer to different objects but their contents are all equal.For case 1, jasmine provides the toBe method. This checks for reference. For example, Exampledescribe("Array Equality", () => {    it("should check for array reference equility", () => {       let arr = [1, 2, 3];       let arr2 = arr       // Runs successfully       expect(arr).toBe(arr2);       // Fails as references are not equal       expect(arr).toBe([1, 2, 3]);    }); });OutputThis ... Read More

Advertisements