java参数传值代码举例
更新时间:2024年03月07日 08:23:46 作者:励志当野王
在编程中往方法中传递参数的方法往往有两种,一种是值传递,一种是引用传递,而在java中所有的参数传递全部都是值传递,这篇文章主要给大家介绍了关于java参数传值的相关资料,需要的朋友可以参考下
基本数据类型参数的传值
package Absent;
public class chapter1 {
public static void main(String[] args) {
Computer com=new Computer();
int b=100;
int a=12;
int result=com.add(b, a);
System.out.println(result);
result=com.add(b*12+2,a*12+3);
System.out.println(result);
}
}
class Computer{
int add(int x,int y) {
return x+y;
}
}引用类型参数的传值
package Absent;
public class Chapter2 {
public static void main(String[] args) {
Abolish abolish=new Abolish(100);
System.out.println("南孚电池的储量是:"+abolish.electricityAmount);
Absolute absolute=new Absolute();
System.out.println("收音机开始使用南孚电池");
absolute.openRadio(abolish);
System.out.println("目前南孚电池的储存量为:"+abolish.electricityAmount);
}
}
class Abolish{
int electricityAmount;
Abolish(int amount){
electricityAmount=amount;
}//构造方法
}
class Absolute{
void openRadio(Abolish abolish) {
abolish.electricityAmount=abolish.electricityAmount-10;
}//消耗的电量
}总结
到此这篇关于java参数传值的文章就介绍到这了,更多相关java参数传值内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
Java中ConcurrentHashMap和Hashtable的区别
ConcurrentHashMap 和 Hashtable 都是用于在Java中实现线程安全的哈希表数据结构的类,但它们有很多区别,本文就来详细的介绍一下,感兴趣的可以了解一下2023-10-10
Mybatis中的resultType和resultMap使用
这篇文章主要介绍了Mybatis中的resultType和resultMap使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教2024-09-09
SpringBoot使用Nacos进行application.yml配置管理
Nacos是阿里巴巴开源的一个微服务配置管理和服务发现的解决方案,它提供了动态服务发现、配置管理和 服务管理平台,Nacos使用Raft协议保证配置的一致性,同时支持多种配置 格式,如properties、yaml等,本文介绍了SpringBoot使用Nacos进行application.yml配置管理2024-12-12


最新评论