Java entry (7): method and java Entry Method

Source: Internet
Author: User

Java entry (7): method and java Entry Method

Methods can be understood as solutions to problems or things in daily life. The formation of a method requires consideration and analysis to form a step-by-step process, finally, verify your ideas in the actual execution process. In Java, the formation of methods is also the case.

A method is used to encapsulate a specific logical function, such as percentage calculation, non-null judgment, and time-date conversion. It can be called repeatedly in a program to reduce repeated code, it also facilitates program maintenance and can be equivalent to a small wheel.

A complete method consists of seven parts: access modifier, modifier, return value type, method name, parameter list, exception handling, and method body, which can only be declared inside the class.

Access modifier return value type method name (parameter list) throws exception {

Method body

}

1. Access Modifier

Used to declare the available range of a method. The options include public, protected, private, and default. The public modifier can be accessed by all classes. The protected modification method can be accessed by all classes in the same package, or by all subclasses (different packages can be used. The private modifier can only be accessed by the current class. By default, it is also called a non-access modifier. The modifier can be accessed by all classes in the same package.

2. Modifier

Declares the nature or purpose of a method. Includes static, final, abstract, native, and synchronized. Static modifier is a static method, also called a class method. It can be called directly by class name points. The final modifier method cannot be overloaded (the object-oriented method will carefully reload and override ). Abstract modifier indicates that this method is an abstract method, indicating that the class has been declared but not implemented. The native modifier is not implemented in the class. This modifier is not commonly used. The method modified by synchronized indicates that the method is "locked" and cannot be executed in other places until the method is completed, which will be detailed in the subsequent threads.

3. Return Value Type

The data type used to declare the processing result of the method. If the method does not return a value, you can use void to declare that no value is returned. If the method has a return value, you must use the return statement in the method to return data that is compatible with the return value type.

4. Method Name

Method Identification name. Its naming principles comply with Java Naming rules, so that it can be a little longer.

5. Parameter List

It is used to declare the data to be processed in a method. It is a prerequisite for method execution and can have multiple identical or different data type parameters.

6. Exception Handling

When throws is used to declare exceptions that may occur during method running, it is thrown to the method caller. Common exceptions include NULL pointer exceptions and input and output exceptions.

7. Method body

The method body is the processing process of the method, and the specific algorithm logic or business logic is the core of a method.

Package javalearningday06; import java. util. arrays;/*** Method * @ author Ogawa 94 * @ date July 22, March 25, 2018 */public class Function {public static void main (String [] args) {System. out. println (helloSomeone (""); // Hello world! System. out. println (helloSomeone ("Ogawa 94"); // Hello, Ogawa 94! System. out. println (randomCheckCode (4); // verify int a = 5 for parameters in the method; // declare the local variable a // pass a to the add method, instead of using a that already exists in the stack, it will open up storage space in the stack and store the parameter a add (); // The output prints a as the variable a declared in the main method, instead of passing the parameter a System in the add method. out. println (a); // a = 5}/*** greeting method ** StringUtil is a tool class, which does not need to be referenced in the same package. * For transmitted parameters, if you will perform related operations in the future, be sure to determine whether it is empty * @ param name * @ return */public static String helloSomeone (String name) {if (StringUtil. isNullOrEmpty (name )){ Return "Hello world! ";} Return" Hello, "+ name + "! ";}/*** Generate the num-bit random verification code String * @ param num number of digits * @ return */private static String randomCheckCode (int num) {if (StringUtil. isNullOrEmpty (num) {return "Please input the parameters that need to generate the digit verification code! ";} If (0 = num | num <0) {return" Please input a positive integer greater than 0! ";} String [] dic = {" A "," B "," C "," D "," E "," F "," G ", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q ", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "0 ", "1", "2", "3", "4", "5", "6", "7", "8", "9 "}; string [] strArr = new String [num]; for (int I = 0; I <strArr. length; I ++) {int index = (int) (Math. random () * dic. length); strArr [I] = dic [index];} return Arrays. toString (strArr);}/*** about the variables in the method: * The variables declared in the method and passing parameters in the parameter list are all local variables. During running, all buckets will be allocated in the stack * @ param a */protected static void add (int a) {a ++ ;}}

The following is a tool class for null judgment.

Package javalearningday06; /*** whether the tool class is empty * @ author Ogawa 94 * @ date July 22, March 25, 2018 */public class StringUtil {/*** determines whether the object is null or an empty string * @ param obj * @ return */public static boolean IsNullOrEmpty (Object obj) {if (obj = null) {return true;} if (obj. toString (). trim (). equals ("") {return true;} return false ;} /*** determine whether the Object is not null or an empty string * @ param obj * @ return */public static boolean IsNotEmpty (Object obj) {r Eturn! IsNullOrEmpty (obj );}}

You can also use the method as a machine that can repeat the production product. The access modifier is equivalent to who can operate the machine; the modifier is equivalent to the machine setting, and the product type and weight are set. The return value type is equivalent to the final product. The method name is the name and number of the machine; the parameter list is the raw material of the production product. Exception Handling is equivalent to a failure that may occur during machine operation. The method body is the processing process of the machine. Finally, the return statement is used, to generate the final finished product.

Java's advanced features are object-oriented, which can be used to abstract and process everything. Everything is an object. There are abstract ing, specific inductive abstraction, and mutual conversion, these will not only be used in programming, but also in life.

The above code has been uploaded to GitHub. The address is GitHub! If you have any questions, please leave a message below.

The article was first published on my personal public account:Happy book. I like to share my songs, movies, books, code, and deep-night meditation. Looking forward to your attention!

Enter the keyword"Java learning ebook", You can get 12 books related to Java learning. If the financial ability permits, please also support the author's genuine and paper books, which is not easy to create.

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.