当前位置:网站首页>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);
}
}
边栏推荐
- Study Notes 11--Direct Construction of Local Trajectories
- The thread pool method opens the thread -- the difference between submit() and execute()
- 学习笔记11--局部轨迹直接构造法
- hcip06 ospf特殊区域综合实验
- flowable workflow all business concepts
- Mysterious APT Attack
- By building a sequence table - teach you to calculate time complexity and space complexity (including recursion)
- OC-ARC(Automatic Reference Counting)自动引用计数
- leetcode 剑指 Offer 57. 和为s的两个数字
- idea2021+Activiti [the most complete note one (basic use)]
猜你喜欢
随机推荐
通过构建一个顺序表——教你计算时间复杂度和空间复杂度(含递归)
BERT pre-training model series summary
Re16:读论文 ILDC for CJPE: Indian Legal Documents Corpus for Court Judgment Prediction and Explanation
In the robot industry professionals, Mr Robot industry current situation?
(C language) file operation
(Text) Frameless button settings
Basic operations of sequence table in C language
Online target drone prompt.ml
[100个Solidity使用技巧]1、合约重入攻击
The creation of a large root heap (video explanation)
【 HMS core 】 【 】 the FAQ HMS Toolkit collection of typical questions 1
leetcode 剑指 Offer 52. 两个链表的第一个公共节点
实战演练 | 在 MySQL 中计算每日平均日期或时间间隔
时刻铭记:总有一天你将破蛹而出
If someone asks you about distributed transactions again, throw this to him
Flink_CDC construction and simple use
快解析结合用友时空
PyQt5 - draw sine curve with pixels
PyQt5-绘制不同类型的直线
leetcode 剑指 Offer 10- II. 青蛙跳台阶问题









