The Java for C++ programmers tutorial says that (highlight is my own):
The keyword final is roughly equivalent to const in C++
What does "roughly" mean in this context? Aren't they exactly the same?
What are the differences, if any?
The Java for C++ programmers tutorial says that (highlight is my own):
The keyword final is roughly equivalent to const in C++
What does "roughly" mean in this context? Aren't they exactly the same?
What are the differences, if any?
In C++ marking a member function
const
means it may be called onconst
instances. Java does not have an equivalent to this. E.g.:Values can be assigned, once, later in Java only e.g.:
is legal in Java, but not C++ whereas:
In both Java and C++ member variables may be
final
/const
respectively. These need to be given a value by the time an instance of the class is finished being constructed.In Java they must be set before the constructor has finished, this can be achieved in one of two ways:
In C++ you will need to use initialisation lists to give
const
members a value:In Java final can be used to mark things as non-overridable. C++ (pre-C++11) does not do this. E.g.:
But in C++:
this is fine, because the semantics of marking a member function
const
are different. (You could also overload by only having theconst
on one of the member functions. (Note also that C++11 allows member functions to be marked final, see the C++11 update section)C++11 update:
C++11 does in fact allow you to mark both classes and member functions as
final
, with identical semantics to the same feature in Java, for example in Java:Can now be exactly written in C++11 as:
I had to compile this example with a pre-release of G++ 4.7. Note that this does not replace
const
in this case, but rather augments it, providing the Java-like behaviour that wasn't seen with the closest equivalent C++ keyword. So if you wanted a member function to be bothfinal
andconst
you would do:(The order of
const
andfinal
here is required).Previously there wasn't a direct equivalent of
const
member functions although making functions non-virtual
would be a potential option albeit without causing an error at compile time.Likewise the Java:
becomes in C++11:
(Previously
private
constructors was probably the closest you could get to this in C++)Interestingly, in order to maintain backwards compatibility with pre-C++11 code
final
isn't a keyword in the usual way. (Take the trivial, legal C++98 examplestruct final;
to see why making it a keyword would break code)Let me explain what I understood with an example of switch/case statement.
The values in each case statement must be compile-time constant values of the same data type as the switch value.
declare something like below (either in your method as local instances, or in your class as static variable(add static to it then), or an instance variable.
and
This code will not compile, if
color1
is a class/instance variable and not a local variable. This will compile ifcolor1
is defined as static final (then it becomes static final variable).When it does not compile, you will get the following error
You have some great answers here already, but one point that seemed worth adding:
const
in C++ is commonly used to prevent other parts of the program changing the state of objects. As has been pointed out,final
in java can't do this (except for primitives) - it just prevents the reference from being changed to a different object. But if you are using aCollection
, you can prevent changes to your objects by using the static methodThis returns a
Collection
reference that gives read-access to the elements, but throws an exception if modifications are attempted, making it a bit likeconst
in C++I am guessing it says "roughly" because the meaning of
const
in C++ gets complicated when you talk about pointers, i.e. constant pointers vs. pointers to constant objects. Since there are no "explicit" pointers in Java,final
does not have these issues.According to wikipedia:
Java's
final
works only on primitive types and references, never on object instances themselves where the const keyword works on anything.Compare
const list<int> melist;
withfinal List<Integer> melist;
the first makes it impossible to modify the list, while the latter only stops you from assigning a new list tomelist
.