Explicitly specifying the type of local variables is often not necessary. By allowing developers to omit it, we want to simplify Java development by reducing the required number of formalities, but without sacrificing static typing.
var
- for variable local variablesval
- for immutable (final) local variableslet
- for immutable (final) local variablesauto
- let's not consider this option, okay?final
keyword will still be valid before any of these options, with the result that immutable variables can be declared in any of these ways:
final var
- turns a mutable local variable into immutablefinal val
- here the final modifier is redundant and does not change anythingfinal let
- here the final modifier is redundant and does not change anythingvar
and final var
var
and val
, but final var
and final val
validvar
and let
, but final var
and final let
validDo not usevar
when the type of the expression to the right of the assignment is not obvious.
Do not rely on the name of a variable when determining its type. It may not be true.
var
and val
. However, although it is always useful to study the experience of predecessors, this does not mean that for Java this option would be ideal.
val
or let
not suitable for Java.
public double parSpread(SwapLeg leg) { Currency ccyLeg = leg.getCurrency(); Money convertedPv = presentValue(swap, ccyLeg); double pvbp = legPricer.pvbp(leg, provider); return -convertedPv.getAmount() / pvbp; }
public double parSpread(SwapLeg leg) { val ccyLeg = leg.getCurrency(); Money convertedPv = presentValue(swap, ccyLeg); val pvbp = legPricer.pvbp(leg, provider); return -convertedPv.getAmount() / pvbp; }
final
?
public double parSpread(final SwapLeg leg) { val ccyLeg = leg.getCurrency(); final Money convertedPv = presentValue(swap, ccyLeg); val pvbp = legPricer.pvbp(leg, provider); return -convertedPv.getAmount() / pvbp; }
final
used to designate an immutable variable, and in other places the word val
. It is this mixture that spoils everything and at the same time such a problem is not worth it in Skala and Kotlin.
final
for each local variable? I am not using it. But I know that this is a very reasonable standard, invented to improve development security and reduce errors.)
public double parSpread(final SwapLeg leg) { final var ccyLeg = leg.getCurrency(); final Money convertedPv = presentValue(swap, ccyLeg); final var pvbp = legPricer.pvbp(leg, provider); return -convertedPv.getAmount() / pvbp; }
final
continues to be the ubiquitous mechanism for defining an immutable variable. And if you, like me, do not consider it necessary to sign final
everywhere, you simply delete it:
public double parSpread(final SwapLeg leg) { var ccyLeg = leg.getCurrency(); Money convertedPv = presentValue(swap, ccyLeg); var pvbp = legPricer.pvbp(leg, provider); return -convertedPv.getAmount() / pvbp; }
final
existed for many years. If you close your eyes to this fact, the code will turn into an ugly and inconsistent mess.
val
and let
not suitable for Java, because the word final
already exists and it has an understandable meaning. Although the variant with var
and final var
not perfect, I believe that this is the only one of the proposed combinations that fits an existing language.
Source: https://habr.com/ru/post/280188/