Java EE learning route -- two important contents of java basic syntax method: overloading and parameter passing

1. Method overload

1.1 method overload

  • Method overload concept

    Method overloading refers to the relationship between multiple methods defined in the same class. Multiple methods that meet the following conditions constitute overloading with each other

    • Multiple methods in the same class
    • Multiple methods have the same method name
    • Multiple methods have different parameters, different types or different quantities
  • be careful:

    • Overloading only corresponds to the definition of the method and has nothing to do with the method call. The call method refers to the standard format
    • Overloads only identify the names and parameters of methods in the same class, regardless of the return value. In other words, it is not possible to determine whether two methods constitute overloads with each other through the return value
  • Correct example:

    public class MethodDemo {
    	public static void fn(int a) {
        	//Method body
        }
        public static int fn(double a) {
        	//Method body
        }
    }
    
    public class MethodDemo {
    	public static float fn(int a) {
        	//Method body
        }
        public static int fn(int a , int b) {
        	//Method body
        }
    }
    
  • Error example:

    public class MethodDemo {
    	public static void fn(int a) {
        	//Method body
        }
        public static int fn(int a) { 	/*Error reason: overload has nothing to do with the return value*/
        	//Method body
        }
    }
    
    public class MethodDemo01 {
        public static void fn(int a) {
            //Method body
        }
    } 
    public class MethodDemo02 {
        public static int fn(double a) { /*Error reason: These are two fn methods of two classes*/
            //Method body
        }
    }
    

2.2 method overload exercise

  • Requirements: use the idea of method overloading to design a method to compare whether two integers are the same, and be compatible with all integer types (byte,short,int,long)

  • Idea:

    ① define the compare() method to compare whether two numbers are the same. Select two int type parameters for the parameters

    ② define the corresponding overload method, change the corresponding parameter type, and change the parameter to two long parameters

    ③ define all overloaded methods, two byte type and two short type parameters

    ④ complete the method call and test the running results

  • code:

    public class MethodTest {
        public static void main(String[] args) {
            //Call method
            System.out.println(compare(10, 20));
            System.out.println(compare((byte) 10, (byte) 20));
            System.out.println(compare((short) 10, (short) 20));
            System.out.println(compare(10L, 20L));
        }
    
        //int
        public static boolean compare(int a, int b) {
            System.out.println("int");
            return a == b;
        }
    
        //byte
        public static boolean compare(byte a, byte b) {
            System.out.println("byte");
            return a == b;
        }
    
        //short
        public static boolean compare(short a, short b) {
            System.out.println("short");
            return a == b;
        }
    
        //long
        public static boolean compare(long a, long b) {
            System.out.println("long");
            return a == b;
        }
    
    }
    

2. Parameter transfer of method

2.1 basic types of method parameter transfer

  • Test code:

    package com.itheima.param;
    
    public class Test1 {
        /*
             Method parameters are passed as basic data types:
    
                    The value passed into the method is a specific value
         */
        public static void main(String[] args) {
            int number = 100;
            System.out.println("call change Before method:" + number);
            change(number);
            System.out.println("call change After method:" + number);
        }
    
        public static void change(int number) {
            number = 200;
        }
    }
    
    
    
  • Conclusion:

    • The change of basic data type parameters and formal parameters does not affect the actual parameters
  • Basis of conclusion:

    • Each method has an independent stack space in the stack memory. After the method runs, it will pop up and disappear

2.2 method parameter transfer reference type

  • Test code:

    package com.itheima.param;
    
    public class Test2 {
        /*
             Method parameters are passed as reference data types:
    
                    The passed in method is the memory address
         */
        public static void main(String[] args) {
            int[] arr = {10, 20, 30};
            System.out.println("call change Before method:" + arr[1]);
            change(arr);
            System.out.println("call change After method:" + arr[1]);
        }
    
        public static void change(int[] arr) {
            arr[1] = 200;
        }
    }
    
  • Conclusion:

    • For parameters of reference type, the change of formal parameters will affect the value of actual parameters
  • Basis of conclusion:

    • Referring to the parameters of the data type, the address value is passed in. In memory, two references will point to the same memory. Therefore, even if the method bounces the stack, the data in the heap memory is the result of the change

Keywords: Java JavaEE

Added by jakeklem on Mon, 03 Jan 2022 09:09:58 +0200