The use and recursive algorithm of Java language methods

Source: Internet
Author: User

1. What is a method (function)

The Java language approach is similar to a function in other languages, a piece of code that accomplishes a particular function,

2. Statement of methods of mastery

Declaration format: Modifier return value type method name (parameter list) {

program code;

return value is returned;

}

Parameters in the method:

Formal parameters: Used to receive data from outside input when the method is invoked.

Actual parameter: The data that was actually passed to the method when the method was called.

The return value in the method (return value type: The data type of the result to be returned by the method, such as a method that does not return a value, must be given the return value type void)

(return value: The data that the method returns to the caller after execution has completed.) )

(The return statement terminates the run of the method and specifies the data to be returned.) )

3. Mastering the Call of the method

The Java language uses the following form to invoke the method:

The name of the object variable. Method name (argument list);

The number of arguments, the data type and order must match the parameter list of the called method declaration.

The code is as follows:

  

public class demo11{

public static void Printinfor () {

for (int i = 0; i<10; i++) {
System.out.println (i);
}

}
public static int Getmax (int i, int j) {

int max=0;
if (i>j) {
Max=i;
}
else {
Max=j;
}
return Max;

}

public static void Main (String [] args) {

System.out.println ("First time printing");
Printinfor ();
System.out.println ("Second-time printing");
Printinfor ();
System.out.println ("compare two numbers output larger number:");
int res = Getmax (2,4);
System.out.println (RES);


}


}

    

4. Mastering what is a method of overloading

Overloading of methods refers to multiple methods in a class that can be defined with the same name but with different parameter lists, and when called, the corresponding method is selected according to the different parameter lists;

A parameter list refers to the type, number, or order of parameters.

An overload that satisfies a point that is a method:

1. Occurs in the same class

2. Method names are the same

3. Different parameter list (type, size, quantity)

The code is as follows:

public class demo11{
public static int Getmax (int i, int j) {

int max=0;
if (i>j) {
Max=i;
}
else {
Max=j;
}
return Max;

}
public static int Getmax (int i, int j, int k) {
int max= (I&GT;J)? I:j;
if (max>k) {
return Max;
}
else{
return k;
}

}

public static void Main (String [] args) {
System.out.println ("compare two numbers output larger number:");
int res1 = Getmax (2,4);
System.out.println (RES1);
System.out.println ("compare three numbers output larger tree:");
int res2 = Getmax (3,5,4);
System.out.println (Res2);

}


}

5. Understanding Recursive algorithms

1. Recursive invocation refers to the invocation of the method itself that occurs during method execution.

2. The key to recursive algorithms is to capture:

Recursive exit

Push Step to exit approximation

The code is as follows:

  

public class Demo12
{
public static int multip1 (int n)//method that generally asks for factorial.
{
int res = 1;
for (int i=1; i<=n; i++)
{
Res*=i;
}
return res;
}
public static int multip2 (int n)
{
if (n==1 | | n==0)
{
return 1;
}
Return n*multip2 (n-1);
}
public static void Main (String [] args)
{
System.out.println (MULTIP1 (3));
System.out.println (MULTIP2 (7));
}
}

      

  

The use and recursive algorithm of Java language methods

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.