Some strange grammar of Jjava

Source: Internet
Author: User

=============================
Java double colon:: Meaning of the syntax
=============================
The following code has a double colon, which looks strange at first,
List.foreach (System.out::p rintln);

This is the Java8 "method reference", the syntax is the class name:: Static method name, generally used as a simplified LAMDA expression.
See discussion: https://www.zhihu.com/question/28565691
Like what:
List.foreach (System.out::p rintln) equivalent to List.foreach (I->SYSTEM.OUT.PRINTLN (i))
Namelist.foreach (person::new) equivalent to Namelist.foreach (name->new person (name))

=============================
Java Three dots ... Meaning of the parameter
=============================
Java 1.5 introduces three points ... syntax, you can change the parameter syntax. Since it is a mutable parameter syntax, it can only be used for function parameter definitions, and it can only be used in the last parameter. In the function body, the variable parameter can be considered an array; In the context of a function call, you can pass in an array argument, or 0 arguments, or multiple arguments.

A variable parameter function is more flexible than a normal array parameter function. See Example:

 Public classapp{Static voidtest1 (string...names) { for(String name:names) {System.out.println (name); } System.out.println ("End of Test1"); }    Static voidtest2 (string[] names) { for(String name:names) {System.out.println (name); } System.out.println ("End of Test2"); }     Public Static voidMain (string[] args) {test1 (NewString[] {"A", "B"}); Test1 ("A", "B"); Test1 (); //It can even be passed without arguments .Test2 (NewString[] {"A", "B"}); //test2 (); //do not pass arguments will report syntax errorsTest2 (NewString[] {});//An empty array, equivalent to not passing arguments    }}

Some strange grammar of Jjava

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.