[转]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
相关文档:
先来了解一下链表模式的原理:
首先写一个JavaBean,内容是要添加的元素和该元素的节点。
public class NodeBean implements Serializable
{
private Object data; //元素本身
private NodeBean next; //下一个节点
&n ......
最近经常和公司做JAVA的兄弟们打交道,由于需要统加密算法,所以要求做一个加密的DLL文件供兄弟们调用,根据网上各位大虾们的经验,在加上个人在摸索过程中的体会,现在将流程以及个人心得贴上,以供以后漫漫程序生涯参考,如有不对的还请各位大力指正。
第一步是编写java类, ......
8. 当系统处理完成后,报表被显示出来。这是一份商品销售报告,显示的是每类商品的季度销售额以及累计情况。接下来,我们尝试用 Java
创建一个事件响应处理类,把每季度销售额在10万美元以上的数据用红色标示出来。
创建 Java 类
按照如下步骤,用 Java 创建百灵报表(BIRT)的事件响应处理程序。
......
选择的范围太广,可以读的书太多,往往容易无所适从。我想就我自己读过的技术书籍中挑选出来一些,按照学习的先后顺序,推荐给大家,特别是那些想不断提高自己技术水平的Java程序员们。
一、Java编程入门类
对于没有Java编程经验的程序员要入门,随便读什么入门书籍都一样,这个阶段需要你快速的掌握Java基础语法和基本用 ......