If I create a int x = 10
variable and do not change it throughout the code, is it considered a constant? or is only considered a constant if I use a reserved language word for example: final int x = 10 (java)
If I create a int x = 10
variable and do not change it throughout the code, is it considered a constant? or is only considered a constant if I use a reserved language word for example: final int x = 10 (java)
It is not a constant, to be, it could not be altered in any way.
Although, as you put it in the example, it does not change during the code, it is still not a constant because it can be changed at any time.
It would be similar to saying that any member of the class is declared public
, but because it has never been used outside the class, it is considered private
.
In this case, in theory, it is a constant. Because mathematically speaking the variable x does not suffer a change of value in the course of the block of code in which it is, however, when we do a parallel with the programming language Java, we know that there is a reserved word java called 'final', that simplifying for you, it causes the variable to have an unalterable value, giving more excellence to the sense of a constant variable.
Example: final int x = 10
I hope I have helped!