当前位置:网站首页>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);
}
}
边栏推荐
- 自适应控制——仿真实验一 用李雅普诺夫稳定性理论设计自适应规律
- Shell系统学习之数组
- Flink_CDC construction and simple use
- 水电表预付费系统
- 你真的懂Redis的5种基本数据结构吗?
- Re18:读论文 GCI Everything Has a Cause: Leveraging Causal Inference in Legal Text Analysis
- OC-手动引用计数内存管理
- 编译报错: undefined reference to `google::FlagRegisterer::FlagRegisterer解决方法
- Four ways the Metaverse is changing the way humans work
- Study Notes 11--Direct Construction of Local Trajectories
猜你喜欢

Flink_CDC搭建及简单使用

Re16: Read the paper ILDC for CJPE: Indian Legal Documents Corpus for Court Judgment Prediction and Explanation

新一代开源免费的终端工具,太酷了

Materialist Dialectics - Conditionalism

If someone asks you about distributed transactions again, throw this to him

By building a sequence table - teach you to calculate time complexity and space complexity (including recursion)

Re20:读论文的先例:普通法的信息理论分析

百度推广助手遇到重复关键字,验证错误,怎么一键删除多余的

论文阅读:SegFormer: Simple and Efficient Design for Semantic Segmentation with Transformers

Re17:读论文 Challenges for Information Extraction from Dialogue in Criminal Law
随机推荐
(BUG记录)No module named PIL
spark udf 接受并处理 null值.
JVM内存布局、类加载机制及垃圾回收机制详解
(Text) Frameless button settings
Re18:读论文 GCI Everything Has a Cause: Leveraging Causal Inference in Legal Text Analysis
105. 从前序与中序遍历序列构造二叉树(视频讲解!!)
分页 paging
【深度学习】(问题记录)<对一个变量求梯度得到什么>-线性回归-小批量随机梯度下降
北京突然宣布,元宇宙重大消息
shell脚本
shell script
水电表预付费系统
Re15:读论文 LEVEN: A Large-Scale Chinese Legal Event Detection Dataset
[100个Solidity使用技巧]1、合约重入攻击
梅科尔工作室-看鸿蒙设备开发实战笔记四——内核开发
元宇宙改变人类工作模式的四种方式
idea2021+Activiti【最完整笔记一(基础使用)】
再有人问你分布式事务,把这篇扔给他
JCL learning
Re21:读论文 MSJudge Legal Judgment Prediction with Multi-Stage Case Representation Learning in the Real