Methods in Java

Source: Internet
Author: User


Method definition:

complete a function of a separate block of code!

The format of the definition of the method:

the permission modifier static modifier Returns a value of type method name (formal parameter type 1 parameter name 1, formal parameter type 2 parameter Name 2 ...) {

method Body statement;

}

Detailed Introduction:

Permission modifier: The method allows access to the permission range, which can be public. protected. Private can even be omitted. (The difference is explained later)

Static modifier: Static represents a method that can be called directly through the class name. (Sometimes you can also use the modifier final to indicate a method that does not allow child classes to overwrite)

return value type: 1 no return value: The return value type is void

2 have return value: Data type (Temporary use: basic type): 4 Class 8 kinds

Method Name: is the name of the individual code block

parameter type: Data type (basic use basic type)

Parameter name: Name of variable

Method considerations: (For methods that have a return value type)

1) methods and methods are lateral relations

2) method does not call not execute, do not call separately

3) There must be left {curly brackets where there is no semicolon, there is no semicolon where there is no {left curly brace!

4): A method with a return value type, typically called with an assignment!

5) Formal parameters must be followed by the data type, while the actual parameters are called without the data type.

Overloads of the method:

According to the different needs, we have written different methods,

Identifier: Give method/class/interface/variable: from name: see Name and meaning;

Java for this scenario: provides technical------->

Method Overloading (overload): In the same class, the method name is the same, the parameter list is different (parameter type/parameter number), and the return value is not related!

eg

Package org.day04.funcation;


public class Eg {

public static void Main (string[] args) {

int x = 1, y = 2, z = 5;

Double A = 3.14, B = 5.445;

System.out.println (Getsum (y, a));

System.out.println (Getsum (x, y));

System.out.println (Getsum (x, Y, z));


}


public static int Getsum (int a, int b) {

int sum = a + b;

return sum;

}


public static int Getsum (int a, int b, int c) {

int sum = a + B + C;

return sum;

}


public static double Getsum (int A, double b) {

Double sum = a + b;

return sum;

}

}


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.