How downcasting takes place by using the ‘final’ keyword in Java

The JLS has predefined rules for assignment conversion.

If the expression is a constant expression (§15.28) of type byte, short, char, or int:

A narrowing primitive conversion may be used if the type of the
variable is byte, short, or char, and the value of the constant
expression is representable in the type of the variable.

The definition of constant expression:

  • Literals of primitive type and literals of type String
  • Simple names (§6.5.6.1) that refer to constant variables (§4.12.4).
  • The relational operators <, <=, >, and >= (but not instanceof) (§15.20)

The §4.12.4 defines what final variable is

A variable of primitive type or type String, that is final and
initialized with a compile-time constant expression (§15.28), is
called a constant variable.

Leave a Comment

tech