Why does Java Language Specification say that the expression (n > 2) is not a constant expression? -


in java language specifications chapter on definite assignment, example 16-2 says

a java compiler must produce compile-time error code:

{     int k;     int n = 5;     if (n > 2)         k = 3;     system.out.println(k);  /* k not "definitely assigned"                                before statement */  } 

even though value of n known @ compile time, , in principle can known @ compile time assignment k executed (more properly, evaluated). java compiler must operate according rules laid out in section. rules recognize constant expressions; in example, the expression n > 2 not constant expression defined in §15.28.

but, if @ §15.28, says

the relational operators <, <=, >, , >=

can contribute constant expression.

is expression n > 2 constant expression or not? how can determine this?

it says because n not constant expression.

a constant expression expression denoting value of primitive type or string not complete abruptly , composed using following:

and

a constant variable final variable of primitive type or type string initialized constant expression (§15.28).

n not final , therefore isn't constant variable. therefore isn't constant expression. , therefore n < 2 not constant expression.


Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

css - Can I use the :after pseudo-element on an input field? -