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

Oracle ORA-01000 maximum open cursors exceeded

Several days ago, I wrote a piece of program in C#/.Net to import some records into oracle database. It is windows version 10g XE. When the program is running, I got a run-time error which is ORA-01000 maximum open cursors exceeded. Here is the Oracle docs about ORA-01000: ORA-01000 maximum open cursors exceeded Cause: A host language program attempted to open too many cursors. The initialization parameter OPEN_CURSORS determines the maximum number of cursors per user. Action: Modify the program to use fewer cursors. If this error occurs often, shut down Oracle, increase the value of OPEN_CURSORS, and then restart Oracle. In order to solve this problem, I increased my OPEN_CURSORS by using: ALTER SYSTEM SET open_cursors = 5000 SCOPE=BOTH; You can use SQL/PL client and "connect" command to login the database system by using "system" user to change the open_cursors. If you are using .NET, DBA, and Java programming, please remember to close all the re...

Comparison == vs equals() in Java

In java, we have two operations (" == " and " equals() ") to check the equivalence of String s. It is important to understand that the difference between them.    == The == operator compares the two objects and checks to see if two objects are exactly the same object (refer to the same instance). .equals() The equals() method compares the characters in the two different objects. Two strings may be different objects , but can contains the same values (exactly the same characters). public class Main {     public static void main(String[] args) {         String s1 = "Shangshu";         String s2 = new String(s1);         System.out.println(s1 + " == " + s2 + " is " + (s1 == s2));         System.out.println(s1 + ".equals(" + s2 + ") is " + (s1.equals(s2)));     } } The output of the above code is:...

Tips about sizeof() function in C and C++

The unary operator sizeof is used to calculate the sizes of datatypes in Bytes in modern luanguages in the programming languages C and C++. It is an useful operation in memory allocation. In order to use it right, some tips that you may want to know: sizeof() is a compile-time function (macro-like function), not a run-time function. Therefore, you can declare array as: int arr[sizeof(int)]; because sizeof() is a compile-time function, the equation in sizeof() function will not be calculated. For example: int i=3; int a = sizeof(i++); the value of i will not be changed after sizeof(i++);  because sizeof() is a compile-time function, it can not help you determine the size of an array parameter. The following code will print out size 12 and 4. void test_sizeof(int arr[]) { cout << "sizeof(arr) = " << sizeof(arr) << endl; } int main() { int arr[3]; cout << "sizeof(arr) =" << sizeof(arr) << endl; te...