Java declaration effect 'final' for class variables

This is more a matter of theory than a matter of solution, so listen to me.

In C / C ++, as well as PHP, you can declare constants. Usually there are several ways to do this ( #DEFINE for example, or " const type " ...), and the end result of this is that a replacement is performed at compile time, so that all these named constants become literals, This helps, because instead In order to have access to a memory cell for data search, the data is hard-coded, but without feedback from hard-coding - causing a value if it needs to be reused, and changing all instances of this value when it needs to be changed.

But the Java final declaration is a bit incomprehensible; because I can create a class with unset final vars and initialize them when building, that means they are not precompiled as literals, as far as I can tell. With the exception of ensuring that they cannot logically alter the afte construct, does the final declaration make any performance advantage?

Links to articles are in order, if you pay attention to the part that explains what final does, and what is, if there are any advantages, except for stopping the change of value after construction.

As a result, is it possible to declare compilation-level constants in Java in any other way than just using literals (in any case, a bad idea?)

+4
source share
4 answers

Java has constant expressions. See here in the Java language specification.

 A compile-time constant expression is an expression denoting a value of primitive type or a String that does not complete abruptly and is composed using only the following: Literals of primitive type and literals of type String (§3.10.5) Casts to primitive types and casts to type String The unary operators +, -, ~, and ! (but not ++ or --) The multiplicative operators *, /, and % The additive operators + and - The shift operators <<, >>, and >>> The relational operators <, <=, >, and >= (but not instanceof) The equality operators == and != The bitwise and logical operators &, ^, and | The conditional-and operator && and the conditional-or operator || The ternary conditional operator ? : Parenthesized expressions whose contained expression is a constant expression. Simple names that refer to constant variables (§4.12.4). Qualified names of the form TypeName . Identifier that refer to constant variables (§4.12.4). 

But in Java, unlike C / C ++, you also have a JIT compiler, so additional inlay is possible. So, on the bottom line, don't worry until you see the real performance issue.

+5
source

Java has constants that the Java compiler will replace with its values ​​at compile time. For example, member variables that are final and that are of type String are actually constants that are replaced in this way. (This is allowed because the String class is immutable). One consequence of this is that if you change the line in the source code but do not recompile the classes that use this line, these classes will not see the new value of the line.

JLS explains this in the following paragraphs:

4.12.4 final variables

13.4.9 final Fields and Constants

+4
source

End fields are for creating immutable objects.

Static finite fields are your constants.

Compiler optimization, data flow analysis, to some extent. Try javap to see jvm byte codes - if you are interested.

+2
source

Does the final declaration provide any benefit to efficiency?

Not really. This is because the JIT can often determine that the value does not change at runtime and may consider it to be final. (What is the problem if the value is not mutable and changes in another thread)

In Java 8, you can use local variables in closure if they can be final, instead of declaring them final.

+1
source

Source: https://habr.com/ru/post/1433833/


All Articles