首页 > 编程 > Java > 正文

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

2019-11-26 13:46:33
字体:
来源:转载
供稿:网友

如下所示:

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

以上就是小编为大家带来的String类型传递是值传递,char[]类型传递是引用传递的实现的全部内容了,希望对大家有所帮助,多多支持武林网~

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表