Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java SE 综合讨论区 » 实战错误讨论  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 Re:高手来看看这个程序,结果我想不明白 [Re:HenryShanley]
234aini





发贴: 43
积分: 0
于 2004-11-16 18:42 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
HenryShanley wrote:
java中参数传递是pass by value而在c#中是可选,一旦是pass by value,传递的值只会影响到方法内,不会影响到方法外。但是primitive type肯定会被改变,因为在内存中的位置不一样。
但是如果一个引用包含多重引用,eg多维数组,多重引用会被影响到。
这里StringBuffer和String类又不同,一个immutable类,一个mutable类,如果你换成String结果又不同。看下一段

public class Foo
{
public static void main(String[] args)
{
String a=new String("A");
String b=new String("B");
operate(a,b);
System.out.println(a+","+b);
}

static void operate(String x,String y){
x+=y;
y=x;
System.out.println(x+" "+y);
}
}


输出结果是:AB,AB
:A,B
这又是为什么呢?




话题树型展开
人气 标题 作者 字数 发贴时间
12041 高手来看看这个程序,结果我想不明白 (pass StringBuffer as parameter) 小米CS 399 2004-11-14 23:13
10657 Re:高手来看看这个程序,结果我想不明白 dorrenchen 1050 2004-11-14 23:51
10628 Re:高手来看看这个程序,结果我想不明白 小米CS 215 2004-11-15 09:50
10610 Re:高手来看看这个程序,结果我想不明白 小米CS 141 2004-11-15 10:14
10916 Re:高手来看看这个程序,结果我想不明白 JiafanZhou 709 2004-11-15 21:31
10778 Re:高手来看看这个程序,结果我想不明白 234aini 620 2004-11-16 18:42
10732 Re:高手来看看这个程序,结果我想不明白 (pass StringBuffer as parameter) dorrenchen 674 2004-11-15 22:56
10732 Re:高手来看看这个程序,结果我想不明白 (pass StringBuffer as parameter) xiemoyong2008 387 2004-11-16 16:45
10730 Re:高手来看看这个程序,结果我想不明白 (pass StringBuffer as parameter) 234aini 340 2004-11-16 18:38
10426 Re:高手来看看这个程序,结果我想不明白 (pass StringBuffer as parameter) chengbd 137 2004-11-21 20:22
10266 Re:高手来看看这个程序,结果我想不明白 (pass StringBuffer as parameter) wnfwnfff 230 2005-01-28 07:36
10613 Re:高手来看看这个程序,结果我想不明白 (pass StringBuffer as parameter) Reeves1016 181 2005-01-28 13:00

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   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