易截截图软件、单文件、免安装、纯绿色、仅160KB

[转]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


相关文档:

web开发的一些问题(java方向)

 1.      分页的解决方案,通用性,分页时参数丢失问题,如何解决?
      2.      Hibernate优化问题。如何优化数据库访问,使程序访问数据库更优化。
       初用HIBERNATE的人也许都遇到过性能问题,实现 ......

JAVA 动态构建树(2)

 动态构建树的模型和方法:
 
/**
 * 该模块主要是将无规则存放的对象进行一个有组织的树形节点处理
 * @author 夏家吉<jijin0223@126.com>
 *
 */
public class SystemMenuTreeModel {
 
    public SystemMenuTreeModel(){
    & ......

传智播客java培训2010年4月21java高新技术5(泛型)


在传智的这段时间每天的情形想都想得到,两点一线,但是每天都感觉到很充实,学习,不停的学习。越是不停的学习,越是感觉自己的力量是那么的微薄,自己的能力是那么的小,JAVA真是博大精深。老师说叫我们不要担心,可是我不担心也不可能哦,毕竟在同类学校毕业的,也许我们可以算得上是很不错的,但是和我们同台竞技的人 ......

传智播客Java培训 HTML基础

今天是写第一篇传智播客教程日志,也是我看
JavaScript
视频的第一天。
先说缘由吧。大三还有三个月就要结束了,可我还没有感觉到自己能够有足够的能力找到份好工作,大学期间学到的都是些皮毛和理论,我不想到了找工作的时候,面试官问我会
XXX
吗?我说不会。因此我想通过培训增加自己的竞争力。我报了本地的一家
I ......

数据库缓存方案(Java版的,含测试JSP)(转)

http://hi.baidu.com/shedewang/blog/item/b4a71b254e43ce35c895599b.html
说是支持1亿pv/天,也许有点夸张,但如果您能认真看完相信也不会让您失望。
如果大家真想支持我、支持中国人开源项目,请把该文贴到自己的博客中或者收藏本文,记得包含文档的下载地址!!!!!!!谢谢。
我说的系统主要是构建在hibernate之上 ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号