Friday, April 12, 2019

Refresh: (java) final vs immutable

I took this Enki quiz, which I appreciate, and failed. Apparently, I am forgetting simple understandings. Good thing I'm taking these quizzes. Anyhow, here it is:
https://app.enkipro.com/#/public/quiz/5c5b9ca3c50c2917a3d21407/java-final/5ab0dff2552ef519f22d0fe0



What is the difference between using 'final' modifier on an object (or variable) versus using an 'immutable' object (like String)?

In my simple understanding, using 'final' prevents anyone from changing the variable's reference (or address). On a 'final' object, can change the object's values (per attribute via methods) and can NOT change the object's reference (i.e. new <object_type>). When using 'final' on an attribute (of an object), essentially makes the attribute a constant and its value is immutable.

For in-depth explanation, see the answer on GeeksForGeeks:
https://www.geeksforgeeks.org/final-vs-immutability-java/

No comments:

Post a Comment