【Java课程心得】Java中方法的定义与使用和形参与实参的概念


一、定义方法的通用格式

讲解:
         修饰符  返回值类型  方法名( 参数列表 ){
                     方法体;
                     return 结果;(不一定有)
               }

修饰符:  固定写法 public static

返回值类型:
     void: 没有返回值类型
     基本数据类型: byte, short, char, int, long, float, double, boolean
     引用数据类型: String

方法名: 取名字,小驼峰,第一个单词全小写,后面的每一个单词首字母大写 getMaxAge

参数列表: (数据类型 变量名, 数据类型 变量名)
                       就是定义变量

方法体: 要执行的代码

return 结果: 返回结果(不一定有)

例如:

public class Demo02 {
    
    
    public static void main(String[] args) {
    
    
        System.out.println("Hello World");
        return;
    }
}

二、三种方法的定义

1.无参数无返回值

题目: 现在有个公司,有很多程序员都需要计算10和20的和并输出这个功能。

代码如下(示例):

public class Demo031 {
    
    
    // 拓展:main方法是JVM来调用的
    public static void main(String[] args) {
    
    
        // 2.调用方法
        add();
    }
    // 2.定义一个方法
    public static void add() {
    
    
        int a = 10;
        int b = 20;

        int x = a + b;
        System.out.println("结果: " + x);
        return; // 如果方法的返回值是void,可以写 return; 或者省略return
    }
}


2.有参数无返回值

题目: 求任意两个整数的和并输出到控制台。

代码如下(示例):

public class Demo032 {
    
    
    public static void main(String[] args) {
    
    
        add(10, 5);

        System.out.println("-----------");
        add(2, 3);

        System.out.println("-----------");
        add(10, 5);
    }

    // 求任意两个整数的和并输出到控制台
    public static void add(int a, int b) {
    
    
        int c = a + b; // 10 + 5;
        System.out.println("两个数相加: " + c);
    }
}

小结: 参数列表,就是定义变量,调用方法时可以传递数据过来


3.有参数有返回值

题目: 求任意两个整数的和,并能够返回求和结果继续使用。

代码如下(示例):

public class Demo033 {
    
    
    public static void main(String[] args) {
    
    
        int sum = add(1, 2);
        sum += 20;
        System.out.println("sum = " + sum);
    }

    // 求任意两个整数的和,并能够返回求和结果继续使用。
    public static int add(int a, int b) {
    
    
        int c = a + b; // c = 3;
        return c;
    }
}

小结: 1.方法有返回值的好处
           可以保存返回值,后续可以对返回值进行处理


四、定义方法的注意事项

①定义位置,类中方法外面。方法不能嵌套
②调用方法时写错方法名字。
③调用方法时写错了参数列表。
④返回值类型,必须要和 return 语句返回的类型相同。
    如果返回值类型是void,就不能声明return语句。
⑤不能在 return 后面写代码, return 意味着方法结束,所有后面的代码永远不会执行,属于无效代码。

代码如下(示例):

public class Demo04 {
    
    
    public static int test04() {
    
    
        int c = 1 + 2;
        return c; // 1.结束方法  2.将结果返回给调用的地方
        // System.out.println("666");
        // 5.不能在 return 后面写代码, return 意味着方法结束,所有后面的代码永远不会执行,属于无效代码。
    }

    // 4.如果返回值类型是void,就不能声明return语句。
    public static void test03() {
    
    
        // void: 表示空,也就是没有返回值, 可以省略return;不写
        return;
    }

    // 4.返回值类型,必须要和 return 语句返回的类型相同。
    public static double test02() {
    
    
        return 3.3;
    }

    public static void main(String[] args) {
    
    
        // 红色找不到
        // tset(1, 2); // 调用方法时写错方法名字。
        // test(); // 少写了参数
        // test(1, 2, 3); // 多写了参数
        // test(true, false); // 参数类型不匹配
    }

    // 类中方法外
    public static void test(int a, int b) {
    
    

    }
}

五、调用方法的三种形式

调用方法:
     有返回值的方法有三种调用形式
     没有返回值的方法只有一种调用方法

     有返回值的方法有三种调用形式

     直接调用: add(1, 2);
     赋值调用: int x = add(2, 3);
     输出调用: System.out.println(add(2, 3));

     没有返回值的方法只有一种调用方法
      直接调用: test();

代码如下(示例):

public class Demo06 {
    
    
    public static void main(String[] args) {
    
    
        // 直接调用: 方法名(); 没有处理返回的结果
        add(1, 2);

        // 赋值调用: 数据类型 变量名 = 方法名();  保存返回值,方便后续处理
        int x = add(2, 3);
        x += 2;
        System.out.println("x = " + x); // x = 5

        // 输出调用: System.out.println(方法名()) 打印返回值
        System.out.println(add(2, 3));

        System.out.println("--------------");
        // 注意:没有返回值的方法
        // 直接调用
        test();

        // 不能赋值调用
        // void y = test();

        // 不能输出调用
        // System.out.println(test());
    }

    // 没有返回值的方法
    public static void test() {
    
    
        System.out.println("我就是没有返回值");
    }

    // 有返回值的方法
    public static int add(int a, int b) {
    
    
        int c = a + b;

        return c; // 5
    }
}

六、形参与实参

概念:
   形参: 形式参数
   实参: 实际参数

public class Demo07 {
    
    
    public static void main(String[] args) {
    
    
        int x = 5;

        // 实参: 位置调用方法时的参数, 有具体值
        add(x, 2);
    }

    // 形参: 位置定义方法时的参数, 没有具体值
    public static void add(int a, int b) {
    
    
        int c = a + b;
        System.out.println(c);
    }
}

七、方法使用的例子

1.使用方法重载完成比较两个整数是否相同并调用方法(方法重载后续会讲)

代码示例:

public class Demo022 {
    
    
		public static void main(String[] args) {
    
    
			System.out.println(compare((byte)1, (byte)2));
			System.out.println(compare((short) 2, (short) 6));
			System.out.println(compare(3, 8));
			System.out.println(compare(3L, 8L));
		}

		// 比较byte是否相同, 相同true, 不相同false
		public static boolean compare(byte b1, byte b2) {
    
    
			/*if (b1 == b2) {
				return true;
			} else {
				return false;
			}*/
			return b1 == b2;
		}

		// 比较short是否相同
		public static boolean compare(short s1, short s2) {
    
    
			return s1 == s2;
		}

		// 比较int是否相同
		public static boolean compare(int i1, int i2) {
    
    
			return i1 == i2;
		}

		// 比较long是否相同
		public static boolean compare(long l1, long l2) {
    
    
			return l1 == l2;
		}
	}

2.使用方法完成数组遍历并调用方法

需求:
设计一个方法用于数组遍历,要求遍历的结果是在一行上的。例如:[11, 22, 33, 44, 55]

定义方法两个明确
1.明确返回值类型,就是结果的类型, 遍历打印结果.不需要返回值
2.明确参数列表,方法中的未知数据 参数列表(int[] arr)

代码示例:

public class Demo031 {
    
    
		public static void main(String[] args) {
    
    
			//                      0   1   2   3   4
			int[] arr = new int[] {
    
    11, 22, 33, 44, 55}; // 数组的长度是5,最大索引是4,  最大索引是长度-1
			// arr.length(5) - 1 = 最大索引(4)
			printArray(arr);
			printArray(arr);
		}

		// 打印数组内容的方法, a也是指向传递进来的数组      [11, 22, 33, 44, 55]
		public static void printArray(int[] a) {
    
    
			// 1.打印[
			System.out.print("[");

			// 2.遍历数组获取每个元素
			for (int i = 0; i < a.length; i++) {
    
    
				// i = 0    a[0]  11,
				// i = 1    a[1]  22,
				// i = 2    a[2]  33,
				// i = 3    a[3]  44,
				// i = 4    a[4]  55]
				// 3.如果是最后一个元素,打印 元素]
				if (i == a.length - 1) {
    
    
					System.out.println(a[i] + "]"); // println 先打印内容,后换行
				} else {
    
    
					// 4.如果是不是最后一个元素,打印 元素,
					System.out.print(a[i] + ", ");
				}
			}
		}
	}

3.使用方法完成获取数组最大值并调用方法

需求:
设计一个方法用于获取数组中元素的最大值

定义方法2个明确:
1.明确返回值类型: 返回数组的最大值, 返回值类型 int
2.明确参数列表: 获取哪个数组的最大值,不确定, 参数列表: (int[] a)

代码示例:

public class Demo032 {
    
    
		public static void main(String[] args) {
    
    
			int[] arr = new int[] {
    
    8, 9, 1, 3, 2, 6};

			int x = getMax(arr);
			System.out.println("数组最大值为: " + x);
		}

		public static int getMax(int[] a) {
    
    
			// 1.定义一个变量保存较大值,这个变量默认值为数组0索引的数据
			int max = a[0]; // 8

			// 2.遍历数组获取每个元素
			for (int i = 1; i < a.length; i++) {
    
    
				// 3.将遍历出的数据和变量比较
				// 如果遍历出的数据大于变量, 将大的值赋值变量
				if (a[i] > max) {
    
    
					max = a[i];
				}
			}

			// 返回最大值
			return max;
		}
	}

八.方法的参数是基本类型和引用类型的区别

1.参数是基本类型: 传递的是具体值(值传递)

调用方法,方法会进入栈中,方法执行完毕会出栈:

public class Demo021 {
    
    
    public static void main(String[] args) {
    
    
        int number = 5;
        System.out.println("number1 = " + number); // 5

        change(number);

        System.out.println("number2 = " + number); // 5
    }

    public static void change(int number) {
    
    
        number = 10;
    }
}

2.参数是引用类型: 传递的是地址(引用传递)

引用类型:
1.数组
2.对象

示例:

public class Demo022 {
    
    
    public static void main(String[] args) {
    
    
        //                      0   1   2
        int[] arr = new int[] {
    
    11, 22, 33}; // int[] arr = 0x111
        System.out.println(arr[1]); // 22

        change(arr); // 0x111

        System.out.println(arr[1]); // 666
    }

    //                          int[] arr = 0x111
    public static void change(int[] arr) {
    
    
        arr[1] = 666;
    }
}

猜你喜欢

转载自blog.csdn.net/maikotom/article/details/109015013