搜尋
首頁Javajava教程Java 中修改函數傳入值:基本型別與參考型別

基本型別與參考型別 基本型別:值存放在局部變數表中,無論如何修改只會修改目前堆疊訊框的值,方法執行結束對方法外不會做任何改變;此時需要改變外層的變量,必須傳回主動賦值。 引用資料型態:指標存放在局部變數表中,呼叫方法的時候,副本引用壓棧,賦值只改變副本的參考。但是如果直接改變副本引用的值,修改了引用地址的對象,此時方法以外的引用此地址對象當然被修改。 (兩個引用,同一個位址,任何修改行為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 中修改函數傳入值:基本型別與參考型別

Java 中修改函數傳入值:基本型別與參考型別

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(&#39;e&#39;, &#39;o&#39;)
     *         returns "mosquito in your collar"
     * "the war of baronets".replace(&#39;r&#39;, &#39;y&#39;)
     *         returns "the way of bayonets"
     * "sparring with a purple porpoise".replace(&#39;p&#39;, &#39;t&#39;)
     *         returns "starring with a turtle tortoise"
     * "JonL".replace(&#39;q&#39;, &#39;x&#39;) 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 

#引用型別會造成淺拷貝和深拷貝現象。

相關文章:

JavaScript修改作用域外變數的方法

PHP閉包函數傳參及使用外部變數的方法,php變數

以上是Java 中修改函數傳入值:基本型別與參考型別的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
如何將Maven或Gradle用於高級Java項目管理,構建自動化和依賴性解決方案?如何將Maven或Gradle用於高級Java項目管理,構建自動化和依賴性解決方案?Mar 17, 2025 pm 05:46 PM

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

如何使用適當的版本控制和依賴項管理創建和使用自定義Java庫(JAR文件)?如何使用適當的版本控制和依賴項管理創建和使用自定義Java庫(JAR文件)?Mar 17, 2025 pm 05:45 PM

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

如何使用咖啡因或Guava Cache等庫在Java應用程序中實現多層緩存?如何使用咖啡因或Guava Cache等庫在Java應用程序中實現多層緩存?Mar 17, 2025 pm 05:44 PM

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

如何將JPA(Java持久性API)用於具有高級功能(例如緩存和懶惰加載)的對象相關映射?如何將JPA(Java持久性API)用於具有高級功能(例如緩存和懶惰加載)的對象相關映射?Mar 17, 2025 pm 05:43 PM

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

Java的類負載機制如何起作用,包括不同的類載荷及其委託模型?Java的類負載機制如何起作用,包括不同的類載荷及其委託模型?Mar 17, 2025 pm 05:35 PM

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

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
4 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
4 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
4 週前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
1 個月前By尊渡假赌尊渡假赌尊渡假赌

熱工具

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

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

SublimeText3 Mac版

SublimeText3 Mac版

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

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能