I know that Java uses "final" to declare a constant and that c uses "const". Just wondering what the differences are between the two.
Asked
Active
Viewed 1.0k times
6
-
2Just making something `final` in Java doesn't make it a constant. – azurefrog Sep 12 '16 at 22:32
-
And why a tag for a completely unrelated language? – too honest for this site Sep 12 '16 at 22:34
-
In Java final reference is the same as a const pointer (* const) . That is you can change the value of the data the pointer points to but you can't make the pointer point to different data. – Created May 13 '20 at 22:33
1 Answers
13
In java, making something final means that it can't be reasigned to another reference to another instance, but if it's a reference to a mutable class, the mutable values inside the class can still be modified.
For example, a final String is a constant because Strings are immutable in Java, but a final ArrayList means that you cannot assign it to another ArrayList, but you can still add and remove elements to that ArrayList

DrKarl
- 356
- 3
- 8