A method with parameters

Source: Internet
Author: User

I. Methods with parameters

Grammar:

The access modifier method returns a value type method name (parameter list) {

Method body

Note: Split between each parameter with ","

Two. Formal parameters and arguments

Formal parameter: Parameters specified at method definition

Arguments: Parameters specified at method invocation

Three. Issues to be aware of when calling with parameter methods

1. Parameter data types for formal parameters and arguments remain consistent

2. The order of the formal parameters and arguments remains the same

3. The number of formal parameters and arguments remains the same








Public classFruit {/*** What are parameters? * When a method is defined, the variable placed inside the parentheses is the argument (parameter list) * Used between each parameter, split*/ Public voidZhazhi (String fruit,intweight) { /*** Fruit: Apple * weight:1*/System.out.println (Fruit+weight+ "KG"); }} Public classFruittest { Public Static voidMain (string[] args) {//method How to invoke theFruit fruit=NewFruit (); Fruit.zhazhi ("Apple", 1); }}
 Public classseachstudentname {string[] names=NewString[10];  Public voidinit () {names[0] = "Zhao Yiqian"; names[1] = "Money Two"; names[2] = "Magozo"; names[3] = "John Doe"; names[4] = "Friday"; names[5] = "WU Six"; names[6] = "Zheng Qi"; names[7] = "Wang Ba"; names[8] = "Feng"; names[9] = "Chen Shi"; }    //Find student Names     Public BooleanSeachname (String name,intStartintend) {        BooleanFlag =false;  for(inti = start-1; I < end; i++) {            if(names[i].equals (name)) {flag=true;  Break; }        }        returnFlag; }}
ImportJava.util.Scanner; Public classSechtest { Public Static voidMain (String[]args) {seachstudentname stu=NewSeachstudentname ();        Stu.init (); Scanner input=NewScanner (system.in); System.out.println ("Please enter your name:"); String name=Input.next (); System.out.println (Stu.seachname (name,3,6)); if(Stu.seachname (Name, 3, 6) {System.out.println ("I found it."); }Else{System.out.println ("Not Found"); }    }}

A method with parameters

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.