What is the difference between constant variables and final variables in java?

前端 未结 4 835
感情败类
感情败类 2020-12-31 02:27

Please help me understand the difference between constant variables and final variables in Java. I am a bit confused with it.

4条回答
  •  忘掉有多难
    2020-12-31 02:52

    Constant is the concept, the property of the variable.

    final is the java keyword to declare a constant variable.


    As other people pointed out, from a semantic/linguistic point of view the expression constant variable is an oxymoron and, as such, we could argue about its correctness.

    Quoting the specification, anyway, we can read

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

    I suppose, hence, that we can accept (and consider correct) this binomial for our purpose.

提交回复
热议问题