Skip to main content

Java函数参数的传递机制

Java函数参数的传递是以传递值,或者说副本来进行的。

看到上面这句话可能从C/C++转过来的程序员可能会有些疑惑,那岂不是没有引用传递这一说了。那有些函数我需要用到传递引用怎么办呢?

其实不然,Java函数也是可以传递引用的(是不是更迷惑了:))。 其实这要设计到Java变量和类型的存储引用机制。Java对原始变量存值,而对其它对象采用引用操作,类似C/C++的指针。如果我们称后者为引用类型的话。那么但引用类型传递给函数时,Java函数复制一个引用的副本 (这时这个引用的副本可以理解为 指向原对象指针的副本)。这样对这个引用的操作就会实时的反映到原对象上面。也即完成了原来C/C++中传递指针或者引用的功能。

这样就不难理解为什么Java函数参数是传递值的了。

下面给出一个小小的例子:

class Ob {
   int value = 0;
   public Ob(int i) {
     value = i;
   }
}


public class Test {

public void increaseValue(int i){
i += 1;
System.out.println("In function i = " + i);
}

public void increaseValue(Ob ob){
ob.value += 1;

System.out.println("In function ob = " + ob.value);
}

public void increaseValue(Integer it){
it += 1;

System.out.println("In function it = " + it);
}

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

Test test = new Test();

System.out.println("This is a Test");

int i = 0;
Integer it = new Integer(0);
Ob    ob = new Ob(0);

System.out.println("Before Function i = " + i);
test.increaseValue(i);
System.out.println("After Function i = " + i);

System.out.println("Before Function it = " + i);
test.increaseValue(it);
System.out.println("After Function it = " + it);

System.out.println("Before Function ob = " + ob.value);
test.increaseValue(ob);
System.out.println("After Function ob = " + ob.value);
}
}

===================output================
This is a Test

Before Function i = 0
In function i = 1
After Function i = 0

Before Function it = 0
In function it = 1
After Function it = 0

Before Function ob = 0
In function ob = 1
After Function ob = 1

Comments

Popular posts from this blog

(zz) 我为旅行狂---Key West亲情计划 (2009/5/5)

强烈建议各亲朋好友在离开南部之前,到美国最南端的小岛上看一看。体验一下海天相接的极致景致,以及阳光灿烂的明媚心情。给在南部生活的学术生涯点缀一些不一样的心情,留下值得回忆的美好。此次 KW 之行历时 6 天,驱车 38 小时,经过 Pensacola , Miami , Key Largo , Key west , Plam Beach, 以及 Everglades. 除了预计到的长途跋涉辛苦以及晒成麦色的皮肤之外,可以用圆满来形容。特此感谢 YC MM ,其先驱之行起到了至关重要的影响意义。在脑海里只留下一片片的海蓝色之前,先奉献上我们的驱车路线,以便感兴趣的驴友们方便之用。 提前 10 天的时候记得先查天气,千万不要遇到 hurricane 这样的危险情况,这对海边游历是个大忌。我们此次采取西下东上。一路晴空万里,运气甚为好。               4.24           早 6 点出发, 4 个半小时之后到达 pensacola beach. 这片海域的确与众不同,若是以前没有去过,千万不要错过。雪地一般的沙滩,宝石一般的海水,一望无际的天空。最妙的在于那是狭长的海岸,我们 停下车的地方就是小小海道的尽头,像是走进了海的深处,妙不可言。若是允许,最好带上透风较好的帐篷,海风中安睡的感觉值得体验。下午 6 点左右吃过饭,继 续向南前行大概 2 个小时,到达一个不知名小镇安住下来,目的是给明天的 miami 路途节省时间和精力。           4.25           早 9 点出发,史进 Miami 。这段距离大概需要 8 个小时。一路上风景一般,不过还是和家里不同。成片的椰树林也是很养眼的。到达 Miami 的时候已是日暮。若是想去大名鼎鼎的 joy's stone crab 吃著名的 stone crad, 一定记得先去餐厅定上位子。他们生意做的很牛,若是刚好赶上周末,是不给电话定位的。所以要 physically 先去,定上时间。然后去天体浴 场看看裸体的人们以及美丽的日落。差不多华灯...

Java: Syntax error, parameterized types are only available if source level is 5.0

HOW TO FIX IT: It is because eventhough you have Java 5 installed, the JVM running may be different. Try typing this in the console window: java -version. It will give you the Version of the Runtime Environment. Or, if you are using an IDE, then you need to enable Java 5 support. In Eclipse, You can add Java 5 in Window > Preferences > Java > Installed JRE’s. and/or, Set the compiler compliance level to 5.0 (Window > Preferences > Java > Compiler) In Netbeans, you can use (Tools -> Java Platform Manager) to check your default platform. If you use javac commond line, try: javac -1.5 xxx.java Reference: http://laksmono.wordpress.com/2007/05/27/syntax-error-parameterized-types-are-only-available-if-source-level-is-50/

Changing Task Progress By SwingWorker

When SwingWorker is used to report task progress, it should be noted that the propertyChangeEvent won't be invoked if the oldValue is the same as the oldValue. To solve this problem, it is better to reset your progress status to zero or random number before the SwingWorker starts. The setting will force the propertyChangeEvent to be invoked. So your progress status will be updated.