方法 1:方法 1.1 方法概述 **方法(method):**就是完成特定功能的代码块
1.2 方法的定义和调用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 public class MethodDemo { public static void main (String[] args) { isEvenNumber(); } public static void isEvenNumber () { int number = 10 ; number = 9 ; if (number % 2 == 0 ) { System.out.println(number + "是偶数" ); } else { System.out.println(number + "不是偶数" ); } } }
注意事项:
方法定义完毕后,需要调用才能执行
方法必须先定义后调用 ,否则程序将报错
1.4 练习1(输出较大值) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 public class MethodTest { public static void main (String[] args) { int max = getMax(10 , 20 ); System.out.println(max); System.out.println(getMax(10 ,20 )); } public static int getMax (int a,int b) { if (a >= b) { return a; } else { return b; } } }
1.5 带参数方法的定义和调用 格式:
范例:
注意事项:
带参方法定义时,参数中的数据类型 与变量名 都不能缺少,缺少任意一个程序将报错
带参方法定义时,多个参数之间使用逗号 (,)分隔
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 public class MethodDemo { public static void main (String[] args) { isEvenNumber(10 ); isEvenNumber(9 ); int number = 11 ; isEvenNumber(number); number = 12 ; isEvenNumber(number); } public static void isEvenNumber (int number) { if (number % 2 == 0 ) { System.out.println(true ); } else { System.out.println(false ); } } }
参数方法的调用和注意事项:
带参方法调用时,参数的数量与类型 必须与方法定义中的设置相匹配,否则程序将报错
1.6 形参和实参 基本数据类型作为方法参数
参数传递概念:
形式参数 :
在定义方法时,()中定义的变量,称为形式参数,定义时必须不能有值,方法被调用时,才会有值
实际参数:
在调用方法时,()中给定的参数(常量/变量),称为实际参数,如果是变量,则必须要有值
特点: 形式参数的改变,不会影响实际参数
原因:
基本类型作为参数,传递的是具体的数据值
每个方法都有自己的内存空间,所以每个方法内部的变量 也有自己的内存空间,
即便是不同的方法内部定义了同名的变量,他们的内存空间不同,它们之间互不影响,互不干扰
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 public class Demo03BaseVar { public static void main (String[] args) { int a = 10 ; int b = 20 ; System.out.println("ms..a=" +a); System.out.println("ms..b=" +b); change( a, b ); System.out.println("me..a=" +a); System.out.println("me..b=" +b); } public static void change (int a, int b) { System.out.println("cs..a=" +a); System.out.println("cs..b=" +b); a = a * 10 ; b = b * 10 ; System.out.println("ce..a=" +a); System.out.println("ce..b=" +b); return ; } }
1 2 3 4 5 6 7 8 9 10 11 需求:比较两个int 类型的数组是否一样,返回true 或者false 分析: 1. 方法是否需要接收数据进行处理? 因为,方法中需要两个int 数组比较,但是需求并不明确是哪两个数组; 所以,需要接收两个int 类型的数组,形参声明为:int [] arr1,int [] arr2 2. 方法是否需要返回数据? 因为,方法最终的结果需要true 或者false ; 所以,返回值类型是boolean 1. 方法内部的业务:判断两个数组内容是否一样。
代码如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 public class MethodTest4 { public static void main (String[] args) { int [] arr1 = {10 , 20 , 30 }; int [] arr2 = {10 , 20 , 30 }; System.out.println(equals(arr1, arr2)); } public static boolean equals (int [] arr1, int [] arr2) { if (arr1 == null && arr2 == null ){ return true ; } if (arr1 == null || arr2 == null ) { return false ; } if (arr1.length != arr2.length){ return false ; } for (int i = 0 ; i < arr1.length; i++) { if (arr1[i] != arr2[i]){ return false ; } } return true ; } }
1.7 练习2(输出较大值) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 public class MethodTest { public static void main (String[] args) { getMax(10 ,20 ); int x = 10 ; int y = 20 ; getMax(x,y); } public static void getMax (int a, int b) { if (a >= b) { System.out.println("较大的数是:" + a); }else { System.out.println("较大的数是:" + b); } } }
1.8 带返回值方法的定义和调用 下面呢我们来学习带返回值方法的定义和调用。
格式:
范例:
注意事项:
方法定义时return后面的返回值与方法定义上的数据类型要匹配 ,否则程序将报错
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 public class MethodDemo { public static void main (String[] args) { boolean flag = isEvenNumber(10 ); System.out.println(flag); } public static boolean isEvenNumber (int number) { if (number % 2 == 0 ) { return true ; } else { return false ; } } }
调用和注意事项:
方法的返回值通常会使用变量接收 ,否则该返回值将无意义
1.9 练习3(输出较大值) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 public class MethodTest { public static void main (String[] args) { int max = getMax(10 , 20 ); System.out.println(max); System.out.println(getMax(10 ,20 )); System.out.println(getMax(10 , 20 )); } public static int getMax (int a,int b) { if (a >= b) { return a; } else { return b; } } }
1.10 方法的注意事项 Java的方法是在哪个内存区域中执行呢?
答案是栈内存。
每次调用方法,方法都会进栈执行;执行完后,又会弹栈出去。
我们讲解两个注意事项:
方法不能嵌套定义
void表示无返回值,可以省略return,也可以单独的书写return,后面不加数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 方法不能嵌套定义 public static void methodOne () { public static void methodTwo () { } } 返回值类型,必须要和return 语句返回的类型相同,否则编译失败 public static int getSum () { return 5 ; return 1.2 ; return true ; } 不能在return 后面写代码,属于无效代码。 public static int getSum (int a,int b) { return a + b; System.out.println("Hello" ); } void 表示无返回值,可以省略return ,也可以单独的书写return ,后面不加数据public static void methodTwo () { return ; } 定义方法,判断一个字符是否是字母 char c = 'A' ; boolean flag = isChar(c); System.out.println(flag); public static boolean isChar (char ch) { if ((ch>='A' &&ch<='Z' )||(ch>='a' &&ch<='z' )) { return true ; } return false ; }
2:方法的通用格式 2.1 格式 方法定义只有一种格式,这里我们来看一下方法定义的通用格式:
格式:
定义方法时,要做到两个明确
明确返回值类型: 主要是明确方法操作完毕之后是否有数据返回,如果没有,写void;如果有,写对应的数据类型
明确参数: 主要是明确参数的类型和数量
而在调用方法时,我们要知道下面两种不同返回值类型的方法调用:
void类型的方法,直接调用即可
非void类型的方法,推荐用变量接收调用
2.2 练习1(求和) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 public class MethodTest01 { public static void main (String[] args) { int result = sum(5 ); System.out.println("1-5的和是:" + result); result = sum(100 ); System.out.println("1-100的和是:" + result); } public static int sum (int n) { int sum = 0 ; for (int i=1 ; i<=n; i++) { sum += i; } return sum; } }
2.3 练习2(比较相等) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 public class MethodTest02 { public static void main (String[] args) { boolean flag = compare(10 , 20 ); System.out.println("10和20相等吗:" + flag); flag = compare(10 ,10 ); System.out.println("10和10相等吗:" + flag); } public static boolean compare (int a,int b) { return a == b; } }
2.4 练习3(较大值) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 public class MethodTest03 { public static void main (String[] args) { int max = getMax(10 , 20 ); System.out.println("最大值:" + max); System.out.println("main...end..." ); } public static int getMax (int a, int b) { int result = a > b ? a : b; return result; } }
2.5 练习4(判断偶数) 1 2 3 4 5 6 7 8 9 public static boolean isOu (int num) { boolean result = (num % 2 == 0 ) ? true : false ; return result; } int num = 100 ;boolean result = isOu( num );System.out.println(num + " 是偶数? " + result);
2.6 案例 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 int [] arr = new int [3 ]; printArray(arr); fillArray(10 , arr); printArray(arr); public static void printArray (int [] array) { if (array == null ) { System.out.println("null" ); return ; } int maxIndex = array.length - 1 ; if (maxIndex == -1 ) { System.out.println("[]" ); return ; } System.out.print("[" ); for (int i = 0 ; i <= maxIndex; i++) { System.out.print(array[i]); if (i != maxIndex) { System.out.print(", " ); } } System.out.println("]" ); } public static void fillArray (int value, int [] array) { if (array == null || array.length == 0 ) { return ; } for (int i = 0 ; i < array.length; i++) { array[i] = value; } }