Unexpected output when using a ternary operator and final variable

最后都变了- 提交于 2019-12-04 02:21:06

In the second case, z2 counts as a constant expression, because it's a final variable of type int.

From section 4.12.4:

We call a variable, of primitive type or type String, that is final and initialized with a compile-time constant expression (§15.28) a constant variable. Whether a variable is a constant variable or not may have implications with respect to class initialization (§12.4.1), binary compatibility (§13.1, §13.4.9) and definite assignment (§16).

Section 15.28 includes "constant variables" in the set of items which can be used to form a constant expression.

z1 is not a final variable (even though nothing else assigns a value to it) so it's not a constant variable, and thus not a constant expression - so the paragraph you quoted from the spec doesn't apply.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!