日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

在方法参数上使用final关键字

發布時間:2023/12/3 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 在方法参数上使用final关键字 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
經過一些自己的混淆,最終博客方法的具體含義(最終聲明的方法參數)將對此博客條目進行澄清。

至少可以將方法參數上的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

總結

以上是生活随笔為你收集整理的在方法参数上使用final关键字的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。