当前位置:网站首页>方法的参数传递
方法的参数传递
2022-07-30 09:05:00 【抬眼远望】
方法传参
对于基本数据类型的参数,形式参数的改变,不影响实际参数的值
代码实现
public class Test {
public static void main(String[] args) {
int number = 100;
System.out.println("调用change前:"+number);
change(number);
System.out.println("调用change后:"+number);
}
public static void change(int number1){
number1 = 200;
return number1;
}
}
代码解析
public static void main(String[] args) { //把main方法加载到栈内存
int number = 100; //在main方法中加载一个 int number 100;
System.out.println("调用change前:"+number); //将number的值输出
change(number); //调用change方法:把change方法加载到栈内存
public static void change(int number1){ //在chenge方法中,加载一个 int nember1 100
// number1 是形参,它的初始值是实参number给它的。实参number的初始值:100
number1 = 200; // number1的值从100 变为了200
return number1; // 将number1的值返回到main方法中,暂时main方法中没人接收
} //chenge方法结束 int number1 = 200 和change方法一起随着changg方法的结束而在栈内存中消失
System.out.println("调用change后:"+number);//再次输出number的值,这次number的值在main方法中,值为100
} //main方法结束,int number = 100 和main方法一起随着mian方法的结束在栈内存中消失
对于引用类型的参数,形式参数的改变,影响实际参数的值。
代码实现
public class Test {
public static void main(String[] args) {
int[] arr = {10,20,30};
System.out.println("调用change前:"+arr[1]);
change(arr);
System.out.println("调用change后:"+arr[1]);
}
public static void change(int[] arr1){
arr1[1] = 200;
}
}
代码分析
public static void main(String[] args) { //把main方法加载到栈内存
int[] arr = {10,20,30}; //int[] arr 出现在栈内存main方法中 {10,20,30}会出现在堆内存里面,形成2行2列的表格
//表格上方会出现地址值001
//会将地址值001 赋值给栈内存的 arr
System.out.println("调用change前:"+arr[1]);//输出arr[1]的值--->20
change(arr); //调用change方法:把change方法加载到栈内存
public static void change(int[] arr1){在chenge方法中,加载一个 int[] arr1 001
// arr1是arr 传递过来的 也是堆内存中地址值是001的数据
arr1[1] = 200; //通过arr1--->001--->索引1--->20 ===>20变成200 堆内存的数据发生改变
} //chenge方法结束 int number1 001 和change方法一起随着changg方法的结束而在栈内存中消失
System.out.println("调用change后:"+arr[1]);//再次输出arr[1]的值,堆内存的数据发生了改变 但是没有消失
} //main方法结束,int[] arr 001 和main方法一起随着mian方法的结束在栈内存中消失
基本数据类型和引用数据类型数据在传参时区别
基本数据类型,操作传递的是变量的值,改变一个变量的值不会影响另一个变量的值。引用数据类型(类、数组和接口),赋值是把原对象的引用(可理解为内存地址)传递给另一个引用
综合案例
使用带参方法实现学员信息管理
增加学员姓名
在保存了多个学生姓名的数组中,指定查找区间,查找某个学生姓名并显示是否查找成功
package cn.bdqn.demo04;
public class Student {
/*
* 增加学员姓名 在保存了多个学生姓名的数组中,指定查找区间,查找某个学生姓名并显示是否查找成功
*/
// 定义Student类属性
String name;
int age;
// 定义方式实现上述需求:在保存了多个学生姓名的数组中,指定查找区间,查找某个学生姓名并显示是否查找成功
public static boolean searchStudent(Student[] students, int startIndex,
int endIndex, String name) {
int count = 0;
for (int i = startIndex; i <= endIndex; i++) {
if (name.equals(students[i].name)) {
return true;
} else {
count++;
if (count == (endIndex - startIndex + 1)) {
return false;
}
}
}
return false;
}
}
package cn.bdqn.demo04;
public class StudentTest {
public static void main(String[] args) {
//创建Student类对象
Student stu1 = new Student();
stu1.name = "张三";
stu1.age = 20;
Student stu2 = new Student();
stu2.name = "李四";
stu2.age = 20;
Student stu3 = new Student();
stu3.name = "王五";
stu3.age = 20;
Student stu4 = new Student();
stu4.name = "赵六";
stu4.age = 20;
Student stu5 = new Student();
stu5.name = "孙七";
stu5.age = 20;
//将上面5个学生存储到数组中
Student[] stus = {stu1,stu2,stu3,stu4,stu5};
// Scanner sc = new Scanner(System.in);
// System.out.println("请输入你要查找区间的开始下标:");
// int startIndex = sc.nextInt();
// System.out.println("请输入你要查找区间的结束下标:");
// int endIndex = sc.nextInt();
// System.out.println("请输入你要查找的学生姓名:");
// String name = sc.next();
// int count = 0;
// for(int i = startIndex;i<=endIndex;i++){
// if(name.equals(stus[i].name)){
// System.out.println("有你要查找的学生姓名");
// break;
// }else{
// count++;
// if(count==(endIndex-startIndex+1)){
// System.out.println("指定区间内没有你要查找的学生姓名");
// }
// }
// }
boolean result=Student.searchStudent(stus, 1, 3, "赵六");
System.out.println("数组指定区间内有你要查找的姓名:"+result);
}
}
边栏推荐
猜你喜欢
Windows 下安装 MySQL
快解析结合任我行crm
Concise Notes on Integrals - Types of Curve Integrals of the Second Kind
MySQL之COUNT性能到底如何?
信号完整性测试
How to run dist file on local computer
Unreal Engine Graphic Notes: could not be compiled. Try rebuilding from source manually. Problem solving
七大排序之直接选择排序
How to use Jmeter to carry out high concurrency in scenarios such as panic buying and seckill?
Access to display the data
随机推荐
Integral Topic Notes - Path Independent Conditions
Use the R language to read the csv file into a data frame, and then view the properties of each column.
C#中Config文件中,密码的 特殊符号的书写方法。
如何避免CMDB沦为数据孤岛?
团队级敏捷真的没你想的那么简单
Windows 下安装 MySQL
How to run dist file on local computer
【 HMS core 】 【 】 the FAQ HMS Toolkit collection of typical questions 1
echart图表清空上一次数据
342 · Valley Sequence
嘉为鲸翼·多云管理平台荣获信通院可信云技术服务最佳实践
els 方块停在方块上。
els 方块向左移动
LeetCode二叉树系列——94.二叉树的中序遍历
无法定位程序输入点ucrtbase.abort于动态链接库api-ms-win-crt-runtime-|1-1-0.dll上
leetcode 剑指 Offer 22. 链表中倒数第k个节点
Version management of public Jar packages
一个低级错误导致的诡异现象——走近科学能拍三集,(C语言)最简单的数组元素读取,不正确!?
快解析结合象过河erp
实施敏捷过程中这些常见的问题你可曾遇到?