当前位置:网站首页>The method of parameter passing
The method of parameter passing
2022-07-30 10:15:00 【look up】
方法传参
对于基本数据类型的参数,形式参数的改变,不影响实际参数的值
代码实现
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) { //把mainThe method is loaded into stack memory
int number = 100; //在mainmethod to load one int number 100;
System.out.println("调用change前:"+number); //将number的值输出
change(number); //调用change方法:把changeThe method is loaded into stack memory
public static void change(int number1){ //在chenge方法中,加载一个 int nember1 100
// number1 是形参,Its initial value is the actual parameternumber给它的.实参number的初始值:100
number1 = 200; // number1的值从100 变为了200
return number1; // 将number1的值返回到main方法中,暂时mainNo one receives in the method
} //chenge方法结束 int number1 = 200 和changemethod along withchanggThe method ends and disappears in the stack memory
System.out.println("调用change后:"+number);//再次输出number的值,这次number的值在main方法中,值为100
} //main方法结束,int number = 100 和mainmethod along withmianThe end of the method disappears in stack memory
对于引用类型的参数,形式参数的改变,影响实际参数的值.
代码实现
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) { //把mainThe method is loaded into stack memory
int[] arr = {10,20,30}; //int[] arr appears in stack memorymain方法中 {10,20,30}will appear in the heap memory,形成2行2列的表格
//The address value appears above the table001
//will convert the address value001 Assigned to stack memory arr
System.out.println("调用change前:"+arr[1]);//输出arr[1]的值--->20
change(arr); //调用change方法:把changeThe method is loaded into stack memory
public static void change(int[] arr1){在chenge方法中,加载一个 int[] arr1 001
// arr1是arr 传递过来的 Also the address value in heap memory is001的数据
arr1[1] = 200; //通过arr1--->001--->索引1--->20 ===>20变成200 The data in the heap memory has changed
} //chenge方法结束 int number1 001 和changemethod along withchanggThe method ends and disappears in the stack memory
System.out.println("调用change后:"+arr[1]);//再次输出arr[1]的值,The data in the heap memory has changed 但是没有消失
} //main方法结束,int[] arr 001 和mainmethod along withmianThe end of the method disappears in stack memory
Basic data types and reference data types are different when passing parameters
基本数据类型,Actions are passed the value of the variable,Changing the value of one variable does not affect the value of another variable.引用数据类型(类、数组和接口),Assignment takes a reference to the original object(Can be understood as a memory address)passed to another reference
综合案例
Use the method with parameters to achieve student information management
Add student name
in an array that holds multiple student names,指定查找区间,Find a student name and display whether the search was successful
package cn.bdqn.demo04;
public class Student {
/*
* Add student name in an array that holds multiple student names,指定查找区间,Find a student name and display whether the search was successful
*/
// 定义Student类属性
String name;
int age;
// Define the way to achieve the above requirements:in an array that holds multiple student names,指定查找区间,Find a student name and display whether the search was successful
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;
//将上面5students are stored in an array
Student[] stus = {stu1,stu2,stu3,stu4,stu5};
// Scanner sc = new Scanner(System.in);
// System.out.println("Please enter the starting subscript of the interval you are looking for:");
// int startIndex = sc.nextInt();
// System.out.println("Please enter the ending subscript of the interval you are looking for:");
// 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("There is the name of the student you are looking for");
// break;
// }else{
// count++;
// if(count==(endIndex-startIndex+1)){
// System.out.println("The name of the student you are looking for is not found in the specified range");
// }
// }
// }
boolean result=Student.searchStudent(stus, 1, 3, "赵六");
System.out.println("There are names you are looking for in the specified range of the array:"+result);
}
}
边栏推荐
- Array of Shell System Learning
- HR团队如何提升效率?人力资源RPA给你答案
- Re21:读论文 MSJudge Legal Judgment Prediction with Multi-Stage Case Representation Learning in the Real
- 容器技术 -- 简单了解 Kubernetes 的对象
- Day113.尚医通:微信登录二维码、登录回调接口
- 新一代开源免费的终端工具,太酷了
- By building a sequence table - teach you to calculate time complexity and space complexity (including recursion)
- ospf2双点双向重发布(题2)
- 方法的参数传递
- Determine whether a tree is a complete binary tree - video explanation!!!
猜你喜欢

EViews 12.0 software installation package download and installation tutorial

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

HR团队如何提升效率?人力资源RPA给你答案

Security思想项目总结

(***Key points***) Flink common memory problems and tuning guide (1)

Flask's routing (app.route) detailed

Paper reading: SegFormer: Simple and Efficient Design for Semantic Segmentation with Transformers

Online target drone prompt.ml

快解析结合友加畅捷通t1飞跃版

Day113.尚医通:微信登录二维码、登录回调接口
随机推荐
SST-Calib:结合语义和VO进行时空同步校准的lidar-visual外参标定方法(ITSC 2022)
flyway的快速入门教程
Flink_CDC construction and simple use
Soft Exam System Architect Concise Tutorial | Case Analysis | Requirement Analysis
leetcode 剑指 Offer 15. 二进制中1的个数
Re20:读论文 What About the Precedent: An Information-Theoretic Analysis of Common Law
BERT预训练模型系列总结
leetcode 剑指 Offer 10- I. 斐波那契数列
New in GNOME: Warn users when Secure Boot is disabled
线上靶机prompt.ml
606. Create a string from a binary tree (video explanation!!!)
CVTE school recruitment written test questions + summary of knowledge points
leetcode 剑指 Offer 10- II. 青蛙跳台阶问题
唯物辩证法-条件论
Four ways the Metaverse is changing the way humans work
Paper reading: SegFormer: Simple and Efficient Design for Semantic Segmentation with Transformers
Re15: Read the paper LEVEN: A Large-Scale Chinese Legal Event Detection Dataset
Basic operations of sequence table in C language
leetcode 剑指 Offer 63. 股票的最大利润
学习笔记11--局部轨迹直接构造法