软件编程
位置:首页>> 软件编程>> java编程>> String类型传递是值传递,char[]类型传递是引用传递的实现

String类型传递是值传递,char[]类型传递是引用传递的实现

作者:jingxian  发布时间:2022-06-01 09:33:44 

标签:java,string,引用传递

如下所示:


package com.lstc.test;

public class TestDemo3 {
String str = new String("hello");
char[] ch = { 'a', 'b' };

public static void main(String[] args) {
TestDemo3 t = new TestDemo3();
t.change(t.str, t.ch);//String是封装类,是值传递,char数组是引用传递
System.out.println(t.str + " and " + t.ch[0] + t.ch[1]);
}

public void change(String str, char[] ch) {
str = "test ok";
ch[0] = 'c';
}
}

结果是:str任然是hello,ch的第一个元素a变为c

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com