Java - what are methods (functions) in Java

Posted by patmcv on Sat, 29 Jan 2022 19:33:21 +0100

1, Method definition and call

public class Method_Demo1 {
	public static void main(String[] args) {
		print();
	}
	private static void print() {
		for (int i = 0; i < 5; i++) {
			for (int j = 0; j < 8; j++) {
				System.out.print("*");
			}
			System.out.println();
		}
	}
}

1. The print method directly outputs the result after being called by the main method, and the main method does not need the execution result of the print method, so it is defined as void.

2, Format of definition method

Modifier return value type method name(parameter list){
	//Code omission
	return result;
}

matters needing attention

  1. Modifier: public static fixed writing method (in the current class)
  2. Return value type: the data type representing the result of the method operation. After the method is executed, the result is returned to the caller
  3. Parameter list: unknown data in the operation of the method, which is passed when the caller calls the method
  4. Return: bring the result of the method execution to the caller. After the method is executed to return, the overall method operation ends

3, Three forms of calling methods

Direct call: direct write method name call

public static void main(String[] args) {
	print();
}
public static void print() {
	System.out.println("Method called");
}

Assignment call: call a method, define variables in front of the method, and receive the return value of the method

public static void main(String[] args) {
	int sum = getSum(5,6);
	System.out.println(sum);
}
public static int getSum(int a,int b) {
	return a + b;
}

Output statement call:
Invoke the method in output statement, System. out. Println (method name ()).

public static void main(String[] args) {
	System.out.println(getSum(5,6));
}
public static int getSum(int a,int b) {
	return a + b;
}

4, Flow diagram of calling method

5, Considerations for defining methods

  1. Define the location outside the method in the class.
  2. The return value type must be the same as that returned by the return statement, otherwise the compilation fails.
  3. You cannot write code after return. Return means that the method ends and all subsequent codes will never be executed. It is invalid code.

6, Method overloading

  1. Method overloading: more than one method with the same name is allowed in the same class, as long as their parameter lists are different, regardless of modifiers and return value types.
  2. Parameter list: different numbers, data types and orders.
  3. Overloaded method call: the JVM calls different methods through the parameter list of the method.
    Method overload exercise
    Compare whether the two data are equal. The parameter types are two byte types, two short types, two int types and two long types respectively, and are tested in the main method.
public class Method_Demo6 {
public static void main(String[] args) {
	//Define variables of different data types
	byte a = 10;
	byte b = 20;
	short c = 10;
	short d = 20;
	int e = 10;
	int f = 10;
	long g = 10;
	long h = 20;
	// call
	System.out.println(compare(a, b));//false
	System.out.println(compare(c, d));//false
	System.out.println(compare(e, f));//true
	System.out.println(compare(g, h));//false
	}
	// Two byte type
	public static boolean compare(byte a, byte b) {
		System.out.println("byte");
		return a == b;
	}
	// Two types of short
	public static boolean compare(short a, short b) {
		System.out.println("short");
		return a == b;
	}
	// Two int s
	public static boolean compare(int a, int b) {
		System.out.println("int");
		return a == b;
	}
	// Two long type
	public static boolean compare(long a, long b) {
		System.out.println("long");
		return a == b;
	}
}

Determine which methods are overloaded relationships.

public static void open(){}
public static void open(int a){}//yes
static void open(int a,int b){}//yes
public static void open(double a,int b){}//yes
public static void open(int a,double b){}//yes
public void open(int i,double d){}//no
public static void OPEN(){}//no
public static void open(int i,int j){}//Not the same as the third one

Topics: Java