开发者

String constant versus variable

What's the difference between a string constant 开发者_开发百科and a string variable?


Is this what you are looking for?

Java - Variable, Constant and Literal in Java

Variable :

You can assign the values to the variable once it has been declared. The values of the variable can be changed anywhere in the program if the variable is accessible in that scope.

Constants:

Constants are declared using the final keyword. The values of the constant can't be changed once its declared.


In java a constant string means a String variable marked as final;

final String foo = "Some String";

And when a variable is marked as final it can not be changed throughout the program. And a String variable is simply

String bar = "Another String";

It can be changed as many times as you wish. Note also that, whenever you assign a new string literal to a String variable, it does not changes the String, it creates a new one and then assigns it to the existing variable.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜