Methods in Java

Source: Internet
Author: User

concept of the method :

The simple way to do that is to complete the code block for a specific function

benefits of using methods :

Reduce program redundancy, facilitate later maintenance, improve encapsulation

The definition format of the method
Modifier returns a value type method name (data type parameter name 1, data type parameter Name 2,...) {
Method body
return result value;
}
Modifier: public static
Return value type: If the method executes to return data to the caller, the type of the data is the return value type, and if the method executes without returning the data to the caller, use the Void keyword placeholder
Method Name: identifier can be used by method name
Parameter list: Used to receive data passed by the caller, which is similar to defining a variable, and can have 0 or n parameter items.


Method Body :

Code that implements a specific function


Return: keyword, the function is to end the current method, and return the result value to the caller, if this method does not need to return to the caller what data, return can be omitted
Requirements: Define a method that accepts data of two integers, asks for the data of the two integers, and takes this and returns it to the caller
Two aspects
Parameter list: int A, int b, what data is passed to the method, what type of variable is received
return value type: int, the data type to which the method finishes executing the data returned by the caller
After the method definition is complete, if no call is never performed


format of the calling method:
Method name (parameter list);
Actual parameters: When the method's call appears, it represents the data for the method to be worn for short arguments
Formal parameters: When the definition of a method is present, it is used to receive the data for short form parameters passed over

void: Use void placeholder When a method executes without having to return data to the caller


Overloading of methods
Multiple methods with the same name are allowed in Java, but the number of arguments required for the method or the type of the parameter must be different
Benefits: Method name memory becomes simple, the underlying system will automatically invoke the corresponding type of method

To judge overloading:
Look at the method name first, only when the method name is the same, it can be overloaded
Look at the parameter, the number of parameters is different when it must be overloaded
Look at the type, if the method name is the same, the number of arguments is the same, but the type is different that must be overloaded, (the type of the corresponding position)
Overloading is not related to the name of the parameter
Overloading is independent of the return value type

Data types are passed as parameters
When the basic data type is passed as a parameter, the value is passed, the value of the parameter is modified in the method, the data in the core operation is not affected by the original value in two spaces (String also conforms to this article)

The reference type is passed as a parameter of the method, which is the address pass, which is the address number where the data is located, and if the same piece of data is manipulated in the method according to the address number, in the original method
You can see the result after the change.

Define methods to complete array traversal

1/*2*int[] arr = {10,20,30,40,50} 3*4* Inside the method of printing elements according to [10,20,30,40,50]5*/6   Public Static voidShowint[] arr) {7System.out.print ("[");8          for(inti = 0; i < arr.length; i++) {9 Ten             if(I! = arr.length-1) { OneSystem.out.print (Arr[i] + ","); A}Else { -System.out.println (Arr[i] + "]"); -             } the  -         } -}

Methods in Java

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.