2015-10-17
来源:互联网 发布:淘宝店出售 编辑:程序博客网 时间:2024/05/17 08:46
关于java的值传递
public class Example{ String str=new String("good"); char[]ch={'a','b','c'}; public static void main(String args[]){ Example ex=new Example(); ex.change(ex.str,ex.ch); System.out.print(ex.str+" and "); Sytem.out.print(ex.ch); } public void change(String str,char ch[]){ str="test ok"; ch[0]='g'; } }
结果为good and gbc;
网上查了很多资料,不是很懂,不过结论就是传入的参数为基本类型时,实参就不改变,传入为非基本类型时,实参随形参改变。在例子中,string为特殊的基本类型(网上说的),所以不改变,而虽然char是基本类型,可传入的是char[]数组!所以不是基本类型,那实参就变。附上基本类型表:
float 4 字节 32位IEEE 754单精度
double 8 字节 64位IEEE 754双精度
byte 1字节 -128到127
short 2 字节 -32,768到32,767
int 4 字节 -2,147,483,648到2,147,483,647
long 8 字节 -9,223,372,036,854,775,808到9,223,372,036, 854,775,807
char 2 字节 整个Unicode字符集
boolean 1 位 True或者false
补充一下,在Java中,String是一个引用类型,但是在作为参数传递的时候表现出来的却是基本类型的特性
0 0
- 2015-10-17
- 2015-10-17
- 旅行 2015-10-17
- 旅行 2015-10-17
- 旅行 2015-10-17
- 旅行 2015-10-17
- 2015/10/17偶得
- 2015/10/17日想到的
- 心浮气躁(2015-10-17,GDKY)
- 2015年10月17日
- 2015年10月17日
- 2015年10月17日
- 2015-10-17在北京第四届Elasticsearch中国大会
- 学习日记——2015年10月17日
- 10-17
- 10-17
- 2015-1-17完成任务
- 3/17/2015
- Java中的线程技术
- 51nod 1091 线段的重叠 (贪心)
- 你不知道的JavaScript--Item9 call(),apply(),bind()与回调
- AJAX的优缺点
- python学习之正则表达式应用
- 2015-10-17
- VC ON_CONTROL_RANGE多个控件响应一个方法
- NSNumber与NSInteger的区别
- Ubuntu中安装和配置Samba服务
- Codevs P1655 物流运输
- leetcode : 4 Median of Two Sorted Arrays
- ACM学习-综合题-填字游戏
- Reverse Bits
- 队列