[转]Does Java pass by reference or pass by value?
http://www.javaworld.com/javaworld/javaqa/2000-05/03-qa-0526-pass.html
Does Java pass by reference or pass by value?
Why can't you swap in Java?
By Tony
Sintes, JavaWorld.com, 05/26/00
Print
Email
Feedback
Resources
Discuss
(76)
Digg
Reddit
SlashDot
Stumble
del.icio.us
Technorati
dzone
If Java uses the pass-by reference, why
won't a swap function work?
Your question demonstrates a common error made by
Java language newcomers. Indeed, even seasoned veterans find it
difficult
to keep the terms straight.
Java does manipulate objects by reference, and
all object variables are references. However, Java doesn't pass method
arguments
by reference; it passes them by value.
Take the badSwap()
method for
example:
public void badSwap(int var1, int var2)
{
int temp = var1;
var1 = var2;
var2 = temp;
}
When badSwap()
returns, the
variables passed as arguments will still hold their original values. The
method will also fail if we change
the arguments type from int
to Object
,
since Java passes object references by value as well. Now, here is
where it gets tricky:
public void tricky(Point arg1, Point arg2)
{
arg1.x = 100;
arg1.y = 100;
Point temp = arg1;
arg1 = arg2;
arg2 = temp;
}
public static void main(String [] args)
{
Point pnt1 = new Point(0,0);
Point pnt2 = new Point(0,0);
System.out.println("X: " + pnt1.x + " Y: " +pnt1.y);
System.out.println("X: " + pnt2.x + " Y: " +pnt2.y);
System.out.println(" ");
tricky(pnt1,pnt2);
System.out.println("X: " + pnt1.x + " Y:" + pnt1.y);
System.out.println("X: " + pnt2.x + " Y: " +pnt2.y);
}
If we execute this main()
method,
we see the following ou
相关文档:
1. 分页的解决方案,通用性,分页时参数丢失问题,如何解决?
2. Hibernate优化问题。如何优化数据库访问,使程序访问数据库更优化。
初用HIBERNATE的人也许都遇到过性能问题,实现 ......
备注:
本文选自ChinaITLab网校课程《刘晓涛Java就业直通班V2.0》之预备知识,点击这里了解更多!
JAVA学习之路---学习路线及要点
2.软件开发学习路线
两千多年的儒家思想孔孟之道,中庸的思想透入骨髓,既不冒进也不保守并非中庸之道,而是找寻学习软件开发的正确路线与规律。
从软件开发人员的生涯规划来讲,我们可 ......
因数据安全,需要将生成的报表xls,添加水印,所以自已在jxl中进行代码修改。
使用例子
public class testJxl {
public static void main(String[] args) throws Exception {
OutputStream out = new FileOutputStream("./aaaa.xls"); // 写入到FileInputStream
WritableWorkbook wwb= Workbook.createWorkbook(out);
......
本贴转自:http://zhengxuezhou.javaeye.com/blog/540815
利用Sun公司提供的JavaMail
API可以很方便的开发邮件发送程序。也许你已经可以利用它来发送一段简单的文本了,但想不想使你的程序像OUTLOOK一样也能发送附件呢?本文在简单
介绍了JavaMail之后,详细讲解了一段完整的送信的JavaBean及一个十分轻巧的servlet。
......
public class Parent {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void ppublicMethod(){
  ......