Java Object-oriented _ enhanced for variable parameters and code blocks

Source: Internet
Author: User

1.foreach Cycle

For (type variable name: Array or collection) {

Output operation

}

2. Variable parameters: Automatically pass in any number of parameters as required, that is, variable parameters.

Syntax: return value type method name (data type ... Parameter name) {

}

Cases:

1UblicclassPractice14 {2 3     /**4      * @paramargs5      */6      Public Static voidMain (string[] args) {7         //TODO auto-generated Method Stub8         /*string[] name={"Xiao Ming", "Wax Gourd", "Xiao Gang", "Shian"};9 For (String n:name) {Ten System.out.println (n); One         }*/ APrint ("Xiao Ming", "Wax Gourd", "Xiao Gang", "Shian"); -  - } the     //variable parameters (as arrays when used), variable parameters can only have one -      Public Static voidPrint (string...params) { -         //foreach Loop -          for(String s:params) { + System.out.println (s); -         } +     } A}

3. Code block

Common code blocks: code blocks that are written directly in the method

Construct a code block: a block of code defined in a class. Constructs a block of code that executes first, in other words, when a new object is

Static code block: a block of code that uses static declarations in a class

Cases:

1  Public classPractice14 {2     Static{3SYSTEM.OUT.PRINTLN ("Static Fast");4     }5     {6SYSTEM.OUT.PRINTLN ("Fast construction"); 7     }8         9      Public Static voidMain (string[] args) {Ten         //TODO auto-generated Method Stub OnePractice14 test1=NewPractice14 (); APractice14 test2=NewPractice14 (); -  - } the      -}

This example outputs a static fast, two-time construction block

Java Object-oriented _ enhanced for variable parameters and code blocks

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.