开发者

String constant versus variable

开发者 https://www.devze.com 2023-01-30 09:27 出处:网络
What\'s the difference between a string constant 开发者_开发百科and a string variable?Is this what you are looking for?

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

精彩评论

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