Java method overloads and reference array types

Source: Internet
Author: User

1. Method overloading

1) Signature of the method

    • The signature of the method contains the method name and the parameter list
    • In a class, you cannot have two methods with the same signature, that is, the method name and the argument list of a class that cannot have two methods are the same.
public class test{public    void print (int x) {...};    public void print (int x) {...};            Compile error, method signature cannot be the same}public class test{public    void print (int x) {...};    public boolean print (int x) {...};      Compile correctly  }

2) Overloading of methods

In the Java language, multiple methods are allowed to have the same name, but the parameter list is different, called the overloaded (overload) method.

    • Occurs in a class with the same method name and a different argument list
    • The method that the compiler calls automatically at compile time based on the signature binding of the method
public class Sum {public static void main (string[] args) {int x = SUM (5,6);d ouble y = sum (5.0,6.0); SYSTEM.OUT.PRINTLN (x); System.out.println (y);} public static int sum (int x,int y) {return x+y;} public static double sum (double x,double y) {          //method overload return x+y;}} /* Run Result: 1111.0*/

3) Construction method

Construction method is defined in the class method, the construction method is commonly used to implement the initialization of the object member variables, unlike other methods, the construction method is defined by the following two-point rule.

    • The name of the construction method must be the same as the class
    • The construction method does not return a value, but it cannot write void

Grammar:

[Access modifier] class name () {

Construction method body;

}

3.1) The use of the This keyword

The This keyword is used in the method body to point to the current object that invokes the method, simply: Which object invokes the method, this refers to which object, which is used only in the method, and the method accesses the member variable by default. 。

public void Drop () {    this.row + +;    The member variable of the method object is called Row+1, so that the writing concept is better and clearer}//for convenience, you can omit the this:public void Drop () {    row + +) without ambiguity;

In the construction method, the parameters used to initialize the member variable are generally the same name as the member variable, which facilitates the readability of the code, but it is not possible to omit the This keyword, which is used to differentiate member variables and parameters.

Public Cell (int row,int col) {    this.row = row;   Assigns a row in the argument list to a member variable in the class cow    this.col = col;}

3.2) Default construction method

    • Any class must contain a constructor method
    • If there is no definition in the source program, the compiler will add an empty construction method without parameters to it at compile time, which is the default construction method
    • When the constructor method is defined, the Java compiler will no longer add the default constructor method.
Class sum{    int x, y;    Sum (int x,int y) {        this.x = x;        This.y = y;    }} public class test{public    static void Main (string[] args) {        sum s = new Sum ();                Compile error when the constructor method sum (int x,inty) {:} is defined After that, the compiler no longer provides the default constructor method    }}

3.3) Overloading of construction methods

For ease of use, you can define multiple construction methods on a class that have the same name (the class name), and the method's arguments, which are called overloads of the constructor method. A constructor method can invoke another overloaded construction method with the This keyword.

Class sum{    int x, y;    Sum () {        this.x = 0;        This.y = 0;    }    Sum (int x) {        this.x = x;        This.y = x;    }    Sum (int x,int y) {        this.x = x;        thix.y = y;    }} The above constructor method overloads can be overloaded by calling the constructor method, as shown below class sum{    int x, y;    Sum (int x,int y) {        this.x = x;        This.y = y;    }    Sum (int x) {This        (x,x);    }    Sum () {This        (0,0);    }}

  

2. Array of reference types

1) An array is an object

    • In Java, an array belongs to a reference data type
    • Array objects are stored in the heap, array variables are reference types, store the address information of the array object, point to the Array object
    • The elements of an array can be considered as member variables of an array object (except for all types)

2) Declaration of an array of reference types

    • Array elements can be of any type, including reference types

3) Initialization of an array of reference types

    • The default initial value of an array of reference types is null
    • If you want each element to point to a specific object, you need to perform a "new" operation on each of the array elements.
1. Declare the re-assignment emp[] e = new Emp[3];e[0] = new EMP ("Tom", "n"), e[1] = new EMP ("Jerry"), e[2] = new EMP ("Andy", 30),//2. Simultaneous assignment of the Declaration of EM P[] e = new emp[]{new emp ("Tom", "page"), New EMP ("Jerry", "page")    ,    new emp ("Adny")  };

 

4) array type is an array of primitive types

    • The elements of an array can be any type, and also include an array type
int[][] arr = new int[3][];  Arr points to an array that has three elements, each of which is an array of type int, with a length of 2,1,3. Arr[0] = new Int[2];arr[1] = new Int[1];arr[2] = new Int[3];arr[0][0] = 1;arr[0][1] = 2;arr[1][0] = 3;arr[2][0] = 4;arr[2] [1] = 5;arr[2][2] = 6;arr[2][3] = 7;

Java method overloads and reference array types

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.