可變物件具有可以修改的欄位;不可變物件沒有在建立物件後可以更改的欄位。不可變物件是指其狀態在建立後就無法變更的物件。可變和不可變是java中的兩類物件。在本文中,我們將了解 java 中可變物件和不可變物件之間的差異。此外,我們還將看到 Java 程式碼範例,顯示 Java 中可變類別和不可變類別之間的差異。
廣告 該類別中的熱門課程 JAVA 掌握 - 專業化 | 78 課程系列 | 15 次模擬測驗以下是 Mutable 與 Immutable Java 之間的 6 大差異:
以下是 Java 中可變物件和不可變物件之間的主要區別:
讓我們討論 Mutable 與 Immutable Java 之間的主要比較:
可變
Mutable |
Immutable |
Java Mutable object can be modified after its creation. | Java Immutable object cannot be modified after its creation. |
No new object is formed when changes are made to an existing object. | Whenever an existing object is changed, a new object is formed. |
It provides methods to change the content of an object. | It does not provide methods for changing the content of an object. |
Getter and setter methods are present in mutable classes. | Only getter methods are present and not setter methods. |
Mutable classes may or may not be thread-safe. | Immutable classes are thread-safe by default. |
Some common examples of mutable classes in java are StringBuffer, StringBuilder, and java.util.Date. | All Legacy classes, Wrapper classes, String class, are common examples of Immutable classes in java. |
不可變
可變與不可變 Java 的範例如下:
現在我們將看到 Java 程式碼範例,展示了在 Java 中建立可變類別和不可變類別。
代碼:
package com.edubca.mutabledemo; public class MutableDemo { private String studentName; private int rollNumber; MutableDemo (String studentName, int rollNumber) { this.studentName = studentName; this.rollNumber = rollNumber; } public String getStudentName() { return studentName; } // this setter can modify the name public void setStudentName(String studentName) { this.studentName = studentName; } public int getRollNumber() { return rollNumber; } // this setter can modify roll number public void setRollNumber(int rollNumber) { this.rollNumber = rollNumber; } public static void main(String[] args) { MutableDemo obj = new MutableDemo ("John" , 100); System.out.println("Original Name is " + obj.getStudentName()); System.out.println("Original Roll Number is " + obj.getRollNumber()); // update the name, and roll number as is mutable obj.setStudentName("James"); obj.setRollNumber(102); System.out.println("Modified Name is " + obj.getStudentName()); System.out.println("Modified Roll Number is " + obj.getRollNumber()); } }
輸出:
上面的程式碼展示了在java中使用getter和setter創建一個可變類別。
現在我們將了解如何在 Java 中建立不可變類別。
代碼:
package com.edubca.immutabledemo; public final class ImmutableDemo { final String studentName; final int rollNumber; public ImmutableDemo (String studentName, int rollNumber) { this.studentName = studentName; this.rollNumber = rollNumber; } public String getStudentName() { return studentName; } public int getRollNumber() { return rollNumber; } public static void main(String[] args) { ImmutableDemo obj = new ImmutableDemo ("John" , 100); // Since no getters are available contents cannot be modified. // Also as variables are declared final they cannot be modified System.out.println("Name is " + obj.getStudentName()); System.out.println("Roll Number is " + obj.getRollNumber()); } }
輸出:
上面的程式碼顯示了java不可變類別的創建。建立不可變類別時應牢記以下幾點:
透過上面的討論,我們清楚地了解了java中可變和不可變的區別。此外,我們還看到了 Java 範例,展示如何建立可變類別和不可變類別。值得注意的是,不可變類別預設是線程安全的,而可變類別可能是也可能不是線程安全的。
以上是可變與不可變 Java的詳細內容。更多資訊請關注PHP中文網其他相關文章!