Java方法概述,有参无参返回值类型,方法调用,重载
第一章:方法概述
-
方法是什么
方法是一种语法结构,可以把一段代码封装成一个功能,以便重复调用
public class Test { main方法{ } public static int sum(int a, int b) { int c = a+b; return c; } }
优点:
提高代码的复用性;让程序的逻辑更加清晰;
第二章:方法定义格式、常见问题
-
方法完整定义格式
-
修饰符 返回值类型 方法名(形参列表){
方法体代码(需要执行的功能代码)
return 返回值;
}
-
例子
public static int add(int a, int b){ //修饰符 返回值类型 方法名(形参列表) int c = a + b; //方法体代码(需要执行的功能代码) return c; }
-
方法调用格式
方法必须调用才可以跑起来,调用格式: 方法名(。。。);
-
方法格式注意:
方法申明了具体的返回值类型,内部必须使用return返回对应类型的数据。
形参列表可以有多个,甚至没有;如果有多个形参,多个新参必须用","隔开,且不能初始化值。
-
-
方法的其他写法
-
方法定义是:返回值类型、形参列表可按照需求进行填写。
修饰符 返回值类型 方法名(形参列表) {
方法体代码(需要执行的功能代码)
return 返回值;
}
//打印3行hello public class Test01 { public static void main(String[] args) { print(); } public static void print(){ //无参数无返回值的方法 // int i; for (int i = 1; i < 4; i++) { System.out.println("hello"); } } }
-
注意事项
若方法不需要返回结果,返回值类型必须声明为void(无返回值),方法内部不可以使用return返回数据。
方法如果没有参数,或者返回值类型声明为void可以称无参数,无返回值的方法,依此类推。
-
-
方法常见问题
-
方法编写顺序无所谓
-
方法与方法之间是平级关系,不能嵌套定义。
-
方法的返回值类型为void(无返回值),方法内则不能使用return返回数据,如果方法的返回值类型写了具体类型,方法内部则必须使用return返回对应类型的数据。
-
return 语句下面,不能编写代码,因为不能执行,是属于无效代码。
-
方法不调用就不执行,调用时必须严格匹配方法的参数情况。
-
有返回值的方法调用时可以选择定义变量接受结果,或者直接调用;无返回值方法的调用只能调用一下。
package com.itheima.day06Method; public class MethodAttentionDemo3 { public static void main(String[] args) { // 选择定义变量接受结果; int rs = sum(890, 230); System.out.println(rs); //或者输出调用 System.out.println(sum(230, 566)); // 直接调用(只是调用方法,让方法跑一下,但是方法返回值不要了) sum(230, 450); //无返回值方法,只能调用一次 print(); } public static int sum(int a,int b){ return a + b; } public static void print(){ //平级关系 for (int i = 0; i <3 ; i++) { System.out.println("hello"); } } }
-
第三章:方法案例:求和、判断奇偶数
package com.itheima.day06Method;
/**
* @author
*
* 2. 判断整数是奇数还是偶数
* 3. 数组求最值案例改方法实现*/
public class MethodTest {
public static void main(String[] args) {
int reslt = sum(100);
System.out.println("1-100的和是:" + reslt);
// 调用check方法
check(11);
// 求数组的最值
int[] arr = {12, 34, 56, 34, 67, 65};
System.out.println(getArrayMaxData(arr));
}
// 1. 定义方法的技巧、计算1-n的和返回
public static int sum(int n) {
int sum = 0;
for (int i = 0; i <= n; i++) {
sum += i;
}
return sum;
}
// 2. 判断整数是奇数还是偶数
public static void check(int num) {
if (num % 2 == 0) {
System.out.println(num + "是偶数!");
} else {
System.out.println(num + "是奇数!");
}
}
// 数组求最值案例改方法实现
public static int getArrayMaxData(int[] arr) {// 定义数组参数
// 找出数组的最大值返回
int max = arr[0];
// 遍历数组的每个元素与最大值进行比较,若较大则替换
for (int i = 1; i < arr.length ; i++) { // 从索引的第二个值与第一索引比较
if (max < arr[i]) {
max = arr[i];
}
}
return max;
}
}
第四章:方法的内存原理
- 方法没有被调用的时候,在方法区中的字节码文件中存放
- 方法被调用的时候,需要进入到栈内存中运行
第五章:方法传递机制:值传递
-
基本类型的参数传递
-
值传递:在传输实参给方法的形参的时候,并不是传输实参变量本身,而是传输实参变量中存储的值。
-
实参:在方法内部定义的变量
-
形参:在定义方法时,"()"中所声明的参数。
//例子:理解java的基本类型的参数传递:值传递 // 核心代码 int a = 10; change(a); System.out.println(a);//10 public static void change(int a){ System.out.println(a);//10 a = 20; System.out.println(a);//20 }
-
-
引用类型的参数传递
public class HelloWorld { public static void main(String []args) { int[] arrs = {10,20,30}; change(arrs); System.out.println(arrs[1]);// 222 } public static void change(int[] arrs){ System.out.println(arrs[1]);// 20 arrs[1] = 222; System.out.println(arrs[1]);// 222 } }
执行过程
-
补充
基本数据类型的参数传输存储的数据值
引用数据类型的参数传输存储的地址值
第六章:方法参数传递的案例
-
打印数组内容
public class MethodTest1 { public static void main(String[] args) { /* * 需求:设计一个方法用于输出任意整型数组的内容 * 输出如下格式:“该数组内容是:[11,22,33,44,55]”*/ int[] age = {10,20,30,40,50}; printArray(age); System.out.println("==========="); int[] num = {23,45,67, 98, 65, 23, 54}; printArray(num); int[] sum = {1, 322, 45, 565, 78, 34}; printArray(sum); } public static void printArray(int[] arr) { System.out.print("["); for (int i = 0; i < arr.length; i++) { if (arr != null && arr.length > 0) { // 检查数组是否为空 // [10,20,30,40,50] // 0 1 2 3 4 // if (i == arr.length - 1) {//判断数组的最后一位数 3 // System.out.print(arr[i]); // } else { // System.out.print(arr[i] + ","); // } // 优化代码使用三元运算符 System.out.print(i == arr.length - 1 ? arr[i] : arr[i] + ","); } } System.out.println("]"); } }
-
从数组中查询元素的索引返回
需求:设计一个方法可以接受整型数组,和要查询的元素值;最终要返回元素在该数组中的索引,如果数组中不存在该元素则返回 -1.
列如:[11,22,33,44,55] 输入元素44 返回索引为3 输入元素:88,返回 -1;
public class MethodTest2 { public static void main(String[] args) { /*需求:设计一个方法可以接受整型数组,和要查询的元素值;最终要返回元素在该数组中的索引,如果数组中不存在该元素则返回 -1. 列如:[11,22,33,44,55] 输入元素44 返回索引为3 输入元素:88,返回 -1;*/ int[] num = {23, 56, 76, 45, 89, 345, 223}; int index = searchIndex(num,56); System.out.println("您搜索的数据索引是:"+index); int[] arrs = {34, 45, 36, 48, 95, 2362, 38}; System.out.println("您搜索的数据索引是:"+searchIndex(arrs, 67)); } public static int searchIndex(int[] arr, int data) { // 开始寻找这个数的索引 for (int i = 0; i < arr.length; i++) { if (arr[i] == data) { return i; } } System.out.println("数据不存在"); return -1;//无此元素 } }
-
比较两个数组内容是否相等
需求:
如果两个数组的类型,元素个数,元素顺序和内容是一样的我们就认为这两个数组是一模一样的
例如:
public class MethodTest5 { /* * 需求: 如果两个数组的类型,元素个数,元素顺序和内容是一样的我们就认为这两个数组是一模一样的 */ public static void main(String[] args) { int[] arr1 = {10, 20, 30, 40}; int[] arr2 = {10, 20, 30, 40}; int[] arr3 = {12, 34, 56, 40}; System.out.println(compare(arr1, arr3)); } public static boolean compare(int[] arr1, int[] arr2) { // 判断两个数组内容是否一样 if (arr1.length == arr2.length) { for (int i = 0; i < arr1.length; i++) { if (arr1[i] != arr2[i]) { return false; } } return true;//是不相同的 } else { return false; } } }
第七章:方法重载、return关键字
-
方法重载
-
同一个类中,出现多个方法名称相同,但是形参列表是不同的,故这些方法就是重载方法。
// 目标: 识别方法重载的形式,并理解其调用流程,最后需要了解使用方法重载的优点 public class OverLoad { public static void main(String[] args) { // 目标: 识别方法重载的形式,并理解其调用流程,最后需要了解使用方法重载的优点 fire(1); fire("Awm"); fire(10000, "aw"); } // 案例武器发射 public static void fire(int num) { System.out.println("默认发送" + num + "枚到"); } public static void fire(String location) { System.out.println("默认发送枚到" + location); } public static void fire(int num, String Location) { System.out.println("默认发送" + num + "枚到" + Location); } }
-
方法重载的优点
对于相似功能的业务场景:可读性好,方法名称相同提示是同一类型的功能,通过形参不同实现功能差异化的选择。
-
-
方法重载的识别技巧
- 只要是同一个类中,方法名称相同、形参列表不同!(如:修饰符,返回值类型都无所谓!!)
- 形参列表不同指:形参的个数、类型、顺序不同,不关心形参的名称。
补充知识:单独使用return关键字
-
return关键字单独使用
return; --> 可以立即跳出并结束当前方法的执行;
public static void chu(int a, int b){ if (b==0){//判断除数是否为0 System.out.println("您输入数据有错,除数为0!") return; //处理bug哦,跳出当前方法,并结束当前方法执行 } int c = a/ b; System.out.println("结果是:"+c) // return c; }
值类型都无所谓!!)
- 形参列表不同指:形参的个数、类型、顺序不同,不关心形参的名称。
补充知识:单独使用return关键字
-
return关键字单独使用
return; --> 可以立即跳出并结束当前方法的执行;
public static void chu(int a, int b){ if (b==0){//判断除数是否为0 System.out.println("您输入数据有错,除数为0!") return; //处理bug哦,跳出当前方法,并结束当前方法执行 } int c = a/ b; System.out.println("结果是:"+c) // return c; }