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

前端 未结 4 830
感情败类
感情败类 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 03:05

    Make variable "final" means we cannot reassign a value to this variable (ie we can use variable = Something once and only once.

    So for primitive we can say final variable are constante.

    But final variable can be non constant. for exemple

    final Stringbuffer string = new StringBuffer("not"); 
    string.append(" constant");
    System.out.println(string); 
    

    will print the string "not constant". Here "string" variable is final but not constant. i hope this will help

提交回复
热议问题