今天测试了几种java传参,并修改参数的晓测试
测试结果:普通类型传值,其他类型传引用,(String很特别,因为一旦改变值则创建新字符串,在函数改变其值需要注意,可用StringBuffer代替)
代码如下:[size=x-small][/size]
[color=darkblue][/color]
public
class Test
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception
{
List<BaseShop> list = new ArrayList<BaseShop>();
BaseShop baseShop = new BaseShop();
baseShop.setImg("第一次setImg");
baseShop.setImg("
第二次setImg");
for (int i = 0; i < list.size(); i++)
{
baseShop.setShopName("第 "+i+" 店");
list.add(baseShop);
if(list.get(i).getShopName() == null);
System.out.println("list.get(i).getShopName() == null");
}
HashMap map = new HashMap();
map.put("abc", "abc");
int i =0;
StringBuffer string = new StringBuffer("123456789");
TestClass testClass = new TestClass();
testClass.trueValue(map);
testClass.changeInt(i);
testClass.changeString(string);
System.out.println(map.toString());
System.out.println(i);
System.out.println(string);
}
private static class TestClass
{
public TestClass()
{
// TODO Auto-generated constructor stub
}
public Map<String , String> trueValue(Map<String, String> map)
{
map.put("测试哦", "函数添加的");
return map;
}
public int changeInt(int i)
{
i=2;
return i;
}
public StringBuffer changeString(StringBuffer string)
{
string.setLength(0);
string = string.append("函数添加");
return string;
}
}
}