Lambda expression of java8 new characteristic (II.) __java

Source: Internet
Author: User
variables in lambda expressions:

Requirement 1: Create a new method, open a thread in the method, and output 3 "Helloword" in the thread

1. Common wording:

    public static void Main (string[] args) {
        Printhello ("Helloword", 3);
    }
    the final modifier public
    static void Printhello (String name) is used for the bottom of--------------------------------------------//parameter. , int count) {
        //the variable defined here, and the bottom is added the final modifier
        String ch= "!";
        New Thread (New Runnable () {
            @Override public
            void Run () {for
                int i = 0; i < count; i++) {
                    //why inner class You can directly use the external class amount variable ....
                    System.out.println (i+name+ch);
                }
                System.out.println (this);//Print the address of the inner class object, this represents the this for the inner class because this is a static method and the outer class object does not exist
            }
        . Start ();

    -------------------------------------------
    Output Result:
    0helloword!
    1helloword!
    2helloword!
    3helloword!
    4helloword!
    Lambada. TESTLAMBDA$1@2980E8CB//This is the inner class

2. Decompile the above Printhello () method:

    The bottom of the parameter is used with the final modifier public
    static void Printhello (final String name, final int count)
    {
        //the variable defined here, The bottom is also added final modifier
        final String ch = "!";
        (New Thread (new Runnable () {
            private final int val$count;
            Private final String Val$name;
            Private final String val$ch;
            public void Run ()
            {for
                (int i = 0; i < count; i++)
                    System.out.println (New StringBuilder (String.valueo f (i))). Append (name). Append (ch). toString ());

                System.out.println (this);
            }       
            {
                count = i;
                name = S;
                ch = S1;
                Super ();
            }
        }). Start ();
    }

decompile The Printhello () method to know:

1.printHello () The bottom of the parameter is used with final modification.

2. The variables defined in Printhello () are also added to the final modification

3.this is pointing to this of the inner class

2.LAMBDA expression:

    public static void Main (string[] args) {
        new Testlambda (). Printhello ("Helloword", 3);
    }
    The---------------------------------------------
    //parameter is FIANL modified at the bottom, so you can use the free variable  public directly in a lambda expression void Printhello (String name,int count) {
        //local variable defined here, and the bottom is added the final modifier
        String ch= "!";
        New Thread (()->{for
            (int i = 0; i < count; i++) {
                System.out.println (i+name+ch);
            }
            System.out.println (this);/This is not the this of the inner class, but the this of the external class
        . Start ();
    }
    -------------------------------------------
    Output Result:
    0helloword!
    1helloword!
    2helloword!
    Lambada. TESTLAMBDA@663A8EC8//This is an external class

From the printed result you can know:

1.printHello () The bottom of the parameter is used with final modification.

2. The variables defined in Printhello () are also added to the final modification

3. This in the lambda is the 3 types of variables in the method that creates the lambda expression, not the one in the this lambda expression of the inner class:

1. Parameters//For example: in the expression (User o1,user O2) parameter

2. Local variable//For example: variable 3 defined in method

. Free variable (not parameter or local variable)//(for example: A parameter in a method

Summary:

Conclusion:
the free variable in the 1.LAMBDA expression is saved and can be used directly whenever the lambda expression is executed;
2. A free variable cannot be modified in a lambda expression because the free variable is modified by the final
3. Parameters and local variables are used in the same way as normal use; this point in the
4.LAMBDA expression creates this in the method of the lambda expression, not the inner class.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.