可变对象具有可以修改的字段;不可变对象没有在创建对象后可以更改的字段。不可变对象是指其状态在创建后就无法更改的对象。可变和不可变是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中文网其他相关文章!