至少可以將方法參數上的final關鍵字視為Java編譯器的指示符,表明該參數不能重新分配給另一個引用。 Java參數處理始終是“按值調用” (是的,即使在處理對象時也是如此),這是為什么。這是真的。當處理非原始數據類型時,Java處理對對象的引用。 對象本身不會從被調用者傳遞給目標函數! 而是傳遞一個指向所需對象的引用。 但是,此引用不等同于被調用方,因為它只是一個副本。 傳遞給函數的是將復制的引用作為值-好的,每個人都還在嗎? :-)也許Java應該使用更匹配的解釋“按復制參考調用作為值” 。
總結一下:
Java僅以“按值調用”樣式傳遞所有方法參數 (原始數據類型或對對象的引用)!
為了證明這一點,讓我們看一下下面的演示代碼及其輸出。
/*** Call by Value Test Application.** @author Christopher Meyer* @version 0.1 * Apr 21, 2012*/
public class CBVTest {public static void main(String[] args) {Integer mainInternInteger = new Integer(1);/** Even references are copied during calls!** Explanation Objects are never passed, only references to them, BUT* references are copied! So only reference COPIES reach the method.* Neither changes to the reference inside/outside the method will* influence the counterpart.** Maybe it should be called "Call by Copied Reference as Value".*/class RunMe implements Runnable {Integer runnerInternInteger;public RunMe(Integer i) {runnerInternInteger = i;/** The following operation will have no effect on the main* thread, since the reference to "i" is a copied one.* Interfacing the "caller" reference is prevented.*/i = new Integer(3);}@Overridepublic void run() {while (true) {System.out.println(runnerInternInteger.intValue() + "\t (runner intern value)");}}}Thread runner = new Thread(new RunMe(mainInternInteger));runner.start();// Create a new object and assign it to "mainInternInteger".mainInternInteger = new Integer(2);while (true) {System.out.println(mainInternInteger.intValue() + "\t (main intern value)");}}
}
代碼的輸出如下所示:
...
2 (main intern value)
2 (main intern value)
2 (main intern value)
2 (main intern value)
1 (runner intern value)
2 (main intern value)
1 (runner intern value)
2 (main intern value)
1 (runner intern value)
2 (main intern value)
1 (runner intern value)
1 (runner intern value)
1 (runner intern value)
1 (runner intern value)
1 (runner intern value)...
因此,對已處理參數的賦值(i = new Integer(3))或從調用類進行的重新賦值(mainInternInteger = new Integer(2))都不會相互影響。
那么,如果沒有必要,那又有什么價值呢?
如果添加到RunMe的構造方法中(公共RunMe(final Integer i)),則重新分配i = new Integer(3)會引發異常:線程“ main”中的異常java.lang.RuntimeException:無法編譯的源代碼–最終參數我可能不會被分配。 它可以防止因意外重新分配而導致的故障。 意外分配給已處理的參數將始終失敗! 最終迫使開發人員生成準確的代碼。
final關鍵字不是方法簽名的一部分。 因此,無論是否聲明為final,編譯后的代碼都是相同的(每個人都可以使用diff輕松檢查)。 這意味著,一旦聲明了方法參數,就不會重載該方法的參數,而一旦聲明則不會。 由于字節碼保持相同,因此對性能也絕對沒有影響。
更令人困惑的是,請記住,在可以修改變量時(例如,在處理線程的匿名內部類時,如果您不清楚在同一上下文中考慮多個變量,則內部類需要定義一個變量final)。可以更改的相同名稱)。
參考:在Java安全和相關主題博客上 ,使用我們JCG合作伙伴 Christopher Meyer的方法參數上的final關鍵字 。
翻譯自: https://www.javacodegeeks.com/2012/04/using-final-keyword-on-method.html