A method with parameters

Source: Internet
Author: User

I. Methods with parameters:

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

Method body

}

For example: public void Searchname (String name,int start,int end) {

Method body

}

Note: Separate each parameter with ",".

Two. Formal parameters and arguments

Formal parameter: The argument specified when the method is defined.

Arguments: Parameters that are specified when the method is called.

Three. Issues to be aware of when invoking a parametric method: 1. The parameter data types of the formal parameters and arguments are consistent.

2. The order of formal parameters and arguments is consistent.

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

Four. Object array:

For example: Student [] students=new student[30]; An array of student objects with a size of 30 is declared.

//Student name class
Public classStudent {//Create an array of length 10String []names=NewString[10]; //Initialize Public voidinit () {names[0]= "Zhang San"; names[1]= "John Doe"; names[2]= "Harry"; names[3]= "Zhao Liu"; names[4]= "Sun Seven"; names[5]= "Ma Yuan"; names[6]= "Ma Total"; names[7]= "Zhengnan"; names[8]= "Shadow sister"; names[9]= "Shadow Total"; } //Find student Names Public BooleanStudent (String name,intStartintend) { Booleanflag=false; //Cycle Judgment for(inti = start-1; I <end; i++) { if(names[i].equals (name)) {flag=true; Break; } } returnFlag; }}
//Test class
ImportJava.util.Scanner; Public classteststudent { Public Static voidMain (string[] args) {//Creating ObjectsStudent stu =NewStudent (); //Calling MethodsStu.init (); Scanner input=NewScanner (system.in); System.out.println ("Please enter the student's name:"); String name=Input.next (); //Judging if(Stu.student (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.