当前位置:网站首页>方法的参数传递
方法的参数传递
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);
}
}
边栏推荐
- MySQL【运算符】
- 图像分析:投影曲线的波峰查找
- 2022 Hangzhou Electric Multi-School 1st Game
- 自动化测试selenium(一)
- leetcode 剑指 Offer 25. 合并两个排序的链表
- Unity性能分析 Unity Profile性能分析工具
- 连接mysql报错WARN: Establishing SSL connection without server‘s identity verification is not recommended
- 涛思 TDengine 2.6+优化参数
- 代码随想录笔记_哈希_202 快乐数
- LeetCode二叉树系列——94.二叉树的中序遍历
猜你喜欢
![[Fun BLDC series with zero basics] Taking GD32F30x as an example, the timer related functions are explained in detail](/img/1d/700c79a766f115d5d0f3bd8263d67c.png)
[Fun BLDC series with zero basics] Taking GD32F30x as an example, the timer related functions are explained in detail

leetcode 剑指 Offer 25. 合并两个排序的链表

20220728使用电脑上的蓝牙和汇承科技的蓝牙模块HC-05配对蓝牙串口传输

STM8L_库函数-模板搭建

七大排序之直接选择排序

Using IN in MySQL will not go through index analysis and solutions

conda 导出/导出配置好的虚拟环境

C# 之 $ – 字符串内插

微软 SQL 服务器被黑,带宽遭到破坏

Field interpretation under "Surgical variables (RX SUMM-SURG OTH REG/DIS)" in SEER database
随机推荐
Taosi TDengine 2.6+ optimization parameters
leetcode 剑指 Offer 63. 股票的最大利润
经历了这样一个阶段的发展之后,数字零售才能有新的进化
公共Jar包的版本管理
国外资源加速下载器,代码全部开源
积分简明笔记-第二类曲线积分的类型
How to use Jmeter to carry out high concurrency in scenarios such as panic buying and seckill?
MySQL Explain usage and parameter detailed explanation
C#中Config文件中,密码的 特殊符号的书写方法。
积分专题笔记-积分的定义
20220728使用电脑上的蓝牙和汇承科技的蓝牙模块HC-05配对蓝牙串口传输
An article to understand service governance in distributed development
统一异常处理导致ResponseBodyAdvice失效
leetcode 剑指 Offer 52. 两个链表的第一个公共节点
仿牛客网项目第二章:开发社区登录模块(详细步骤和思路)
最长公共序列、串问题总结
Reflection tricks can boost your performance by N times
Integral Special Notes - Definition of Integral
Test automation selenium (a)
大数据产品:标签体系0-1搭建实践