基本型別與參考型別 基本型別:值存放在局部變數表中,無論如何修改只會修改目前堆疊訊框的值,方法執行結束對方法外不會做任何改變;此時需要改變外層的變量,必須傳回主動賦值。 引用資料型態:指標存放在局部變數表中,呼叫方法的時候,副本引用壓棧,賦值只改變副本的參考。但是如果直接改變副本引用的值,修改了引用地址的對象,此時方法以外的引用此地址對象當然被修改。 (兩個引用,同一個位址,任何修改行為2個引用同時生效)
public class Test2 { public static void setValue(String str){ str = "ss"; } public static void setValue(Man str){ str = new Man("test"); } public static class Man{ private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public Man(String name) { this.name = name; } @Override public String toString() { return "Man{" + "name='" + name + '\'' + '}'; } } public static void main(String[] args) { String str = "s"; setValue(str); System.out.println(str); Man man = null; setValue(man); System.out.println(man); } }
如上面程式碼實踐,結果輸出
s null
原因是方法在執行的時候有堆疊幀的概念,入棧的時候只是壓棧方法參數是傳入參數的副本。
Java高階特性
此時區分資料型別:基本型別與參考型別
基本類型:值存放在局部變數表中,無論如何修改只會修改目前堆疊幀的值,方法執行結束對方法外不會做任何改變;此時需要改變外層的變量,必須傳回主動賦值。
引用資料類型:指標存放在局部變數表中,呼叫方法的時候,副本引用壓棧,賦值僅改變副本的參考。 但是如果直接改變副本引用的值,修改了引用地址的對象,此時方法以外的引用此地址對象當然被修改。 (兩個引用,同一個位址,任何修改行為2個引用同時生效)
例如
public static void setValue(StringBuilder str){ str = new StringBuilder("sss"); } public static void setValue2(StringBuilder str){ str.append("sss"); } public static void main(String[] args) { StringBuilder str = new StringBuilder(); setValue(str); System.out.println(str.toString()); setValue2(str); System.out.println(str.toString()); }
關於String,本質是final類型char數組,不可修改,只能賦值,在做參數傳入方法修改時,其實是新建對象,必須回傳重新對外面的變數賦值才會對外面的String引用生效。
看String原始碼的任一方法即可明白
/** * Returns a string resulting from replacing all occurrences of * {@code oldChar} in this string with {@code newChar}. * <p> * If the character {@code oldChar} does not occur in the * character sequence represented by this {@code String} object, * then a reference to this {@code String} object is returned. * Otherwise, a {@code String} object is returned that * represents a character sequence identical to the character sequence * represented by this {@code String} object, except that every * occurrence of {@code oldChar} is replaced by an occurrence * of {@code newChar}. * <p> * Examples: * <blockquote><pre class="brush:php;toolbar:false"> * "mesquite in your cellar".replace('e', 'o') * returns "mosquito in your collar" * "the war of baronets".replace('r', 'y') * returns "the way of bayonets" * "sparring with a purple porpoise".replace('p', 't') * returns "starring with a turtle tortoise" * "JonL".replace('q', 'x') returns "JonL" (no change) ** * @param oldChar the old character. * @param newChar the new character. * @return a string derived from this string by replacing every * occurrence of {@code oldChar} with {@code newChar}. */ public String replace(char oldChar, char newChar) { if (oldChar != newChar) { int len = value.length; int i = -1; char[] val = value; /* avoid getfield opcode */ while (++i
#引用型別會造成淺拷貝和深拷貝現象。
相關文章:
以上是Java 中修改函數傳入值:基本型別與參考型別的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本文討論了使用Maven和Gradle進行Java項目管理,構建自動化和依賴性解決方案,以比較其方法和優化策略。

本文使用Maven和Gradle之類的工具討論了具有適當的版本控制和依賴關係管理的自定義Java庫(JAR文件)的創建和使用。

本文討論了使用咖啡因和Guava緩存在Java中實施多層緩存以提高應用程序性能。它涵蓋設置,集成和績效優勢,以及配置和驅逐政策管理最佳PRA

本文討論了使用JPA進行對象相關映射,並具有高級功能,例如緩存和懶惰加載。它涵蓋了設置,實體映射和優化性能的最佳實踐,同時突出潛在的陷阱。[159個字符]

Java的類上載涉及使用帶有引導,擴展程序和應用程序類負載器的分層系統加載,鏈接和初始化類。父代授權模型確保首先加載核心類別,從而影響自定義類LOA


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

Atom編輯器mac版下載
最受歡迎的的開源編輯器

Dreamweaver CS6
視覺化網頁開發工具

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能