比较一下两程序 帮我分析一下(有关值传递的问题)为什么第一个是引用传递,谢谢...,第二个是值传递

[复制链接]
查看11 | 回复1 | 2010-8-9 09:40:47 | 显示全部楼层 |阅读模式
(1),
public class A
{
public static void mb(String [] s)
{
if(s.length<2)
return;
String t=s[0];
s[0]=s[1];
s[1]=t;
}
public static void main(String args[])
{
String []s={"1","2"};
mb(s);
System.out.print(s[0]+s[1]);
}
}
(2),
public class B
{
public static void mb(String x,String y)
{
x.concat(y);
y=x;
}
public static void main (String args[])
{
String a="A";
String b="B";
mb(a,b);
System.out.println(a+"."+b);
}
}

回复

使用道具 举报

千问 | 2010-8-9 09:40:47 | 显示全部楼层
因为第一个函数的实参是一个数组,传递的是数组的首地址,所以是引用传递。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行