Topic: 关于String的疑惑?怎么ByVal传参的?

  Print this page

1.关于String的疑惑?怎么ByVal传参的? Copy to clipboard
Posted by: frankychen
Posted on: 2005-05-14 17:26

Original Subject: 哪位前辈帮帮我?关于String的疑惑?

class number{
int i;
public number(int i){this.i=i;}
}
public class asd{
public void change(String a,number b){
a="AAAAA";
b.i=200;
}

public static void main(String[] abc)
{
String str = new String("aaaaa");
number num = new number(100);
asd a = new asd();
a.change(str,num);
System.out.println(str);
System.out.println(num.i);
}
}
输出为aaaaa,200。
String不是对象?不是在堆里的么?怎么ByVal传参的?

2.Re:关于String的疑惑?怎么ByVal传参的? [Re: frankychen] Copy to clipboard
Posted by: why
Posted on: 2005-05-14 21:45

Java is always, strictly pass-by-value.

请尽量用准确的文字描述作为标题
"哪位前辈帮帮我?"乃贅文, 廢話, 莫須有.

3.Re:哪位前辈帮帮我?关于String的疑惑? [Re: frankychen] Copy to clipboard
Posted by: lifei1461
Posted on: 2005-05-15 00:23

public class AssignTest {
public long[][] time = null;
public AssignTest() {
  time = new long[4][5];
}

public void setArray(long[][] a) {
//a = new long[2][3];
for (int i = 0; i < a.length; i++) {
for (int j = 0; j <a[i].length; j++) {
a[i][j] = (i+1) * (j+1);
// System.out.println(a[i][j]);
//System.out.println(time1[i][j]);
}
}
}

public void printArray() {

setArray(this.time);//通过这个函数对成员变量进行赋值
for (int i = 0; i < time.length; i++) {
for (int j = 0; j < time[i].length; j++) {
System.out.println(time[i][j]);
}
}
}

public static void main(String[] args) {
AssignTest at = new AssignTest();
at.printArray();
}
}

这程序怎么不是pass-by-value???
输出结果是
1
2
3
2
4
6

4.Re:哪位前辈帮帮我?关于String的疑惑? [Re: lifei1461] Copy to clipboard
Posted by: why
Posted on: 2005-05-15 01:12

lifei1461 wrote:
setArray(this.time);//通过这个函数对成员变量进行赋值
这程序怎么不是pass-by-value???

It's pass-by-value, the value of time; you may think of it as a pointer to the 2-dimensional array.
So you can change the values in the array.


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923