Programming logic - Constant

Asked

Viewed 114 times

0

If I create a variable int x = 10 and not alter it throughout the code it is considered a constant? or is only considered a constant if I use a reserved word of the language for example: final int x = 10 (java)

  • An additional question to make the question more complicated: The compiler can turn a variable into a constant?

2 answers

3

She is not a constant, to be, she could not be altered in any way.

Although, as you put it in the example, it is not changed during the code, still it is not a count, because at any time it can be changed.

It would be similar to say that any member of the class is declared public, but for never having been used outside the class, to be considered private.

2

In your case mathematically, It is a constant. Once a value x does not change over the course of a time line it is considered a constant, but when we make a parallel with the programming language Java(quoted in the question) this answer is not the same. We know that there is a keyword final, that simplifying for you, it makes the variable has an unalterable value, giving more excellence to your code/objective and meaning to the concept of constant.

  • Just because within your code block the variable is unmodified does not mean it will arrive at the end of your unaltered code block. Example: A public variable declared in a library, within the library there may not be a single code that changes the value of that variable, but an external user of that library can make arbitrary changes to its value. The constant statement is not a simplification, the constant statement is a contract that that element will not have its value changed internally or externally to your software.

  • In this case there was a alteration, even being an "external user". Therefore, I made it clear that in theory and mathematically is a constant, since its value will not change: que não muda; inalterável, invariável, fixo.. You mentioned a specific case where the public variable would have its value changed. It is a matter of point of view. However, taking the subject to programming, is not a constant.

  • I cited a case, but I can cite three more cases: A Thread parallel to your code can change the value of the variable, a pipe process can change the value of the variable or a Log installed in the system can manipulate that value. The value of a variable is unpredictable.

  • It’s not that I want to be boring or picky about a concept. It’s that I’ve lost money relying on predictability of variables.

  • I totally agree with you face, and emphasizing my line of thought in the answer, I wanted to demonstrate that mathematically, it is a constant yes and rightly, however, in the world of programming we know that the disuse of the final prevents the realisation of that concept!

  • 1

    I made a change and I believe it was clearer what I wanted to pass in response, I appreciate the feedback @Augustovasques

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.