Domo is Fugito. This time about final variables and immutable objects I tried together.
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.
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