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

来源:互联网 发布:麦淘网源码 编辑:程序博客网 时间:2024/06/05 02:24
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 0