Difference between final and Immutable in Java

What is the difference between final and Immutable in Java?

I recently had the opportunity to use Guava's Immutable List. What's the difference between Immutable and final? I was wondering and examined it.

Features of Immutable

-Prohibit changing the value of the object itself ・ It is possible to change the reference destination

features of final

・ It is prohibited to change the reference destination ・ It is possible to change the value of the variable itself

Now, in Java, let's verify using Immutable's typical String and Mutable StringBuilder.

class Main
{
	public static void main (String[] args) throws java.lang.Exception
	{
		 final String str = "apple";
		 final StringBuilder sb= new StringBuilder("ringo");
		
 		str = str + "ringo"; //Compile error. Of String+Accompanied by changing the reference destination.
                String s = "ringo";
                str = s;             //Compile error
		sb.append("apple");
		sb = new StringBuilder("ringo"); ///Compile error. Change the reference destination.
		System.out.println(str);
		System.out.println(sb);
	}
}

It's relatively simple to compare in this way, but I misunderstood that the value of final was invariant, and surprisingly there was no Japanese article, so I wrote it.

Recommended Posts

Difference between final and Immutable in Java
[Java] Difference between "final variable" and "immutable object"
[Java] Difference between static final and final in member variables
Difference between int and Integer in Java
[Java] Difference between == and equals
Difference between next () and nextLine () in Java Scanner
[Java] Difference between Hashmap and HashTable
[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 ()
Is short-circuit evaluation really fast? Difference between && and & in Java
Difference between pop () and peek () in stack
[For beginners] Difference between Java and Kotlin
Difference between getText () and getAttribute () in Selenium
Difference between "|| =" and "instance_variable_defined?" In Ruby memoization
Arrylist and linked list difference in java
Difference between EMPTY_ELEMENTDATA and DEFAULTCAPACITY_EMPTY_ELEMENTDATA in ArrayList
[Java] Difference between Intstream range and rangeClosed
Difference between vh and%
Difference between i ++ and ++ i
[Java] Understand the difference between List and Set
Distinguish between positive and negative numbers in Java
Understand the difference between int and Integer and BigInteger in java and float and double
Difference between product and variant
[Java] Difference between equals and == in a character string that is a reference type
Difference between redirect_to and render
Rails: Difference between resources and resources
Difference between puts and print
Difference between redirect_to and render
About the difference between classes and instances in Ruby
Difference between new and create in Rais action controller
Difference between CUI and GUI
Difference between variables and instance variables
Difference between mockito-core and mockito-all
[JAVA] What is the difference between interface and abstract? ?? ??
Difference between class and instance
Difference between bundle and bundle install
Difference between ArrayList and LinkedList
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
What is the difference between Java EE and Jakarta EE?
Difference between render and redirect_to
[Java beginner] Difference between length and length () ~ I don't know ~
Difference between Java and JavaScript (how to find the average)
About the difference between "(double quotation)" and "single quotation" in Ruby
Jersey --What is Difference Between bind and bindAsContract in HK2?
Difference between element 0, null and empty string (check in list)
[Java] Difference between assignment of basic type variable and assignment of reference type variable
[Java] Check the difference between orElse and orElseGet with IntStream
Think about the differences between functions and methods (in Java)
Differences in how to handle strings between Java and Perl
[Java] Difference between Stack Overflow Error and Out Of Memory Error
[Ruby] Difference between get and post
Difference between instance method and class method
Difference between interface and abstract class