当前位置:网站首页>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);
}
}
边栏推荐
- Security Thought Project Summary
- 元宇宙改变人类工作模式的四种方式
- Container Technology - A Simple Understanding of Kubernetes Objects
- MySQL |子查询
- PyQt5-绘制不同类型的直线
- The thread pool method opens the thread -- the difference between submit() and execute()
- 一个近乎完美的 Unity 全平台热更方案
- 方法的参数传递
- PyQt5 - draw sine curve with pixels
- 新一代开源免费的终端工具,太酷了
猜你喜欢

Meikle Studio - see the actual combat notes of Hongmeng device development 4 - kernel development

Re19: Read the paper Paragraph-level Rationale Extraction through Regularization: A case study on European Court

In the robot industry professionals, Mr Robot industry current situation?

再有人问你分布式事务,把这篇扔给他

在机器人行业的专业人士眼里,机器人行业目前的情况如何?

mysql安装教程【安装版】

Practical Walkthrough | Calculate Daily Average Date or Time Interval in MySQL

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

leetcode 剑指 Offer 22. 链表中倒数第k个节点

PyQt5 - draw sine curve with pixels
随机推荐
连接mysql报错WARN: Establishing SSL connection without server‘s identity verification is not recommended
OC-手动引用计数内存管理
JVM内存布局、类加载机制及垃圾回收机制详解
(***Key points***) Flink common memory problems and tuning guide (1)
Basic operations of sequence table in C language
快解析结合任我行crm
方法的参数传递
C语言顺序表基本操作
CVTE school recruitment written test questions + summary of knowledge points
学习笔记10--局部轨迹生成主要方法
PyQt5-用像素点绘制正弦曲线
The thread pool method opens the thread -- the difference between submit() and execute()
A near-perfect Unity full-platform hot update solution
105. 从前序与中序遍历序列构造二叉树(视频讲解!!)
606. 根据二叉树创建字符串(视频讲解!!!)
Mysterious APT Attack
In 2022, the top will be accepted cca shut the list
Re19: Read the paper Paragraph-level Rationale Extraction through Regularization: A case study on European Court
leetcode 剑指 Offer 46. 把数字翻译成字符串
Day113. Shangyitong: WeChat login QR code, login callback interface