[Java] Difference between "final variable" and "immutable object"

Domo is Fugito. This time about final variables and immutable objects I tried together.

final variable

A final variable is a variable for which the final modifier is specified. Its nature is called "immutable / read-only" However, strictly speaking, "** non-reassignable variable **".

    final int i = 3;
 i = 5; // Compile error due to reassignment

In the above, final is specified for the basic type (int type) variable i. For basic type variables, the "value itself" is retained in the value of the variable. Variables (For details, see "Assignment of basic type variable and assignment of reference type variable. Differences ”). Therefore, the "value itself" is replaced. Reassignment will result in a compilation error. That is, the basic type As far as variables are concerned, final variables are "immutable" looks like. What's confusing is the final-specified reference variable.

    final StringBuilder sb = new StringBuilder("123");
 sb = new StringBuilder ("456"); // Compile error

    final StringBuilder sb2 = new StringBuiler("789");
 sb.append ("★"); // No compilation error!
 System.out.println (sb2); // "789 ★" is output

In the above example, we are manipulating the reference variables sb and sb2. Here, if you understand that the nature of the final variable is "immutable", Operation to sb results in an error, and operation to sb2 does not. I don't understand the meaning of. Now, if you recall the value held by the reference type variable, This was a "reference to an object". That is, sb and sb2 has "values themselves" such as "123" and "789" is not. What they (?) Have is "" 1 23 (456) "A reference to an object in the state". Consider this mechanism and the strict definition of final variables "non-reassignable" If you put it in, you can understand the above example. That is, The operation "sb = new StringBuilder (" 456 ")" for sb, this Is the operation "** reassign the value of the new reference to sb ". Yo Then, due to the nature of the final variable, a compile error occurs. on the other hand, Operation of "sb2.append (" ★ ")", which has the state "" 789 " Change the state of the object to the state "789 ★" ** " It will be a work. In short, of "reference value to object" to sb2 No reassignment has occurred. Therefore, no error occurs.

Immutable object

While the final variable has the property of "non-reassignable", it is an immutable object. Kut also prohibits "changing the state of objects". In other words The operation of "sb.append (" ★ ")" in the above example is also not possible. It's an immutable object.


** ・ Final variable has the property of "non-reassignable" -If the reference type variable is specified as final, "Change reference destination" is I can't, but "Change the state of the first referenced object Can be changed -Cannot "change the state of the first referenced object" Objects are called "immutable objects" **

... Then this time around here. It was Puyi.

(P.S.) If you have any suggestions for the content of the article Please do not hesitate to let me know m (_ _) m

Recommended Posts

[Java] Difference between "final variable" and "immutable object"
Difference between final and Immutable in Java
[Java] Difference between == and equals
[Java] Difference between static final and final in member variables
[Java] Difference between Hashmap and HashTable
Difference between instance variable and class variable
[JAVA] Difference between abstract and interface
[Java] Difference between array and ArrayList
[Java] Difference between Closeable and AutoCloseable
[Java] Difference between StringBuffer and StringBuilder
[Java] Difference between length, length () and size ()
[Java] Difference between assignment of basic type variable and assignment of reference type variable
[For beginners] Difference between Java and Kotlin
[Java] Difference between Intstream range and rangeClosed
Difference between int and Integer in Java
[Java] Understand the difference between List and Set
[Java] Difference between "final variable" and "immutable object"
static variable
Immutable (immutable) List object conversion function in Java8
[Ruby] "Reference to object" and "Contents of variable"
Difference between vh and%
Difference between i ++ and ++ i
[Ruby] Difference between receiver and object. Differences between Ruby objects and JS objects
About Java setters and getters. <Difference from object orientation>
What is the difference between Java EE and Jakarta EE?
[Java beginner] Difference between length and length () ~ I don't know ~
Rails: Difference between resources and resources
Difference between puts and print
Difference between CUI and GUI
Difference between variables and instance variables
Difference between mockito-core and mockito-all
Difference between bundle and bundle install
Difference between render and redirect_to
Difference between List and ArrayList
java Generics T and? Difference
Difference between .bashrc and .bash_profile
Difference between StringBuilder and StringBuffer
Difference between render and redirect_to
Difference between render and redirect_to
[Docker-compose] Difference between env_file and environment. Priority of environment variable application
Difference between Java and JavaScript (how to find the average)
[Java] Check the difference between orElse and orElseGet with IntStream
[Java] Difference between Stack Overflow Error and Out Of Memory Error
Is short-circuit evaluation really fast? Difference between && and & in Java
[Ruby] Difference between get and post
Difference between instance method and class method
Difference between render method and redirect_to
Difference between == operator and equals method
Differences between "beginner" Java and Kotlin
Java variable declaration, initialization, and types
[Terminal] Difference between irb and pry
JavaServlet: Difference between executeQuery and executeUpdate
[Ruby] Difference between is_a? And instance_of?
Difference between == operator and eqals method
Rough difference between RSpec and minitest
[Rails] Difference between find and find_by
Understand the difference between each_with_index and each.with_index
[Java] Relationship between H2DB and JDBC
Difference between Thymeleaf @RestController and @Controller
Difference between Stream map and flatMap
Difference between primitive type and reference type
Difference between string.getByte () and Hex.decodeHex (string.toCharaArray ())
Differences between Java and .NET Framework
[rails] Difference between redirect_to and render