Git Product home page Git Product logo

java11-lambda-expression-statements's Introduction

Build Status

java11-lambda-expression-statements

https://docs.oracle.com/javase/specs/jls/se11/html/jls-14.html#jls-14.8

https://stackoverflow.com/questions/41482574/lambda-special-void-compatibility-rule-statement-expression

preface

Certain kinds of expressions may be used as statements by following them with semicolons.

  • ExpressionStatement:
    StatementExpression ;
    
  • StatementExpression:
    • Assignment
    • PreIncrementExpression
    • PreDecrementExpression
    • PostIncrementExpression
    • PostDecrementExpression
    • MethodInvocation
    • ClassInstanceCreationExpression

An expression statement is executed by evaluating the expression; if the expression has a value, the value is discarded.

project description

  • assigning a function (boolean add(E e)) to Consumer
    var list = new LinkedList<String>();
    Consumer<String> add = list::add;
    
    add.accept("a");
    
    assertThat(list, hasSize(1));
    
  • function passed as a Consumer
    var source = List.of(1, 2, 3);
    var target = new LinkedList<Integer>();
    
    source.forEach(target::add);
    
    assertThat(target, is(source));
    
  • Consumer that increment the field
    private int x = 5; // field
    
    // then in code
    Consumer<String> increment = s -> x++;
    
    increment.accept("a");
    increment.accept("a");
    increment.accept("a");
    increment.accept("a");
    
    assertThat(x, is(9));
    
  • class creation
    Consumer<Integer> create = X::new;
    
    create.accept(1);
    create.accept(1);
    
    assertThat(X.counter, is(2));
    
    where
    class X {
        static int counter;
    
        X(int i) {
            counter++;
        }
    }
    

java11-lambda-expression-statements's People

Contributors

mtumilowicz avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.