Is final used for optimization in C++?

Is final used for optimization in C++? It can be, and is. As noted, it is being used already; see here and here showing the generated code for the override with and without final. An optimisation along these lines would relate to the “de-virtualization” of the virtual calls. This is not always immediately affected by … Read more

Why attempt to print uninitialized variable does not always result in an error message

In the JLS, §8.3.3. Forward References During Field Initialization, its stated that there’s a compile-time error when: Use of instance variables whose declarations appear textually after the use is sometimes restricted, even though these instance variables are in scope. Specifically, it is a compile-time error if all of the following are true: The declaration of … Read more

Java final modifier

Answering each of your points in turn: primitive variables: can be set only once. (memory and performance gain) Yes, but no memory gain, and no performance gain. (Your supposed performance gain comes from setting only once … not from final.) objects variables: may be modified, final applies to object reference. Yes. (However, this description miss … Read more

Could a final variable be reassigned in catch, even if assignment is last operation in try?

JLS hunting: It is a compile-time error if a final variable is assigned to unless it is definitely unassigned (§16) immediately prior to the assignment. Quoth chapter 16: V is definitely unassigned before a catch block iff all of the following conditions hold: V is definitely unassigned after the try block. V is definitely unassigned … Read more