Java Made Clear: Difference const vs final Keywords

Java Made Clear: Difference const vs final Keywords

const vs final

In Java, const is a reserved word (other only reserved word is "goto") and is not allowed to be used by Programmer in coding. const is supported by C/C++ and const is replaced by final keyword in Java. Other way, C/C++ const keyword equivalent is final in Java. A final variable cannot be reassigned.

In the above code, x is declared final and thereby not allowed to change the value to 20.

Reserved Word

There is a difference between keyword and reserved word. A keyword can be used in coding but a reserved cannot be used. A reserved word usage is reserved in the language itself. In future, the Designers may convert a reserved to a keyword.

Let us go in detail on Keyword vs Reserved word Java

.

  1. Keywords: Keywords has special meaning (functionality) to the compiler. For this reason they cannot be used as identifiers in coding.
  2. Reserved words: When Java was developed Designers wanted to eliminate two words const and goto. Thinking so, they removed them from the Java language and released the first version JDK 1.0 (in the year 1995). But still they were doubtful whether a language can exist without these two words in future; in the sense, they may be required in future versions. For this reason, const and goto were placed in reserved list. That is, they may be brought back into the Java language anytime in future. Right now as on JDK 1.8, Java proved a language can exist without these two words.

    Ofcourse, const place is taken by final keyword in Java. goto is against to structured programming language where with goto, the Programmer looses control over the flow or the structure of coding.

  3. Literals: The literals also cannot be used as identifiers but can be given as values to identifiers.

Some people use keyword and reserved word interchangeably treating as one and the same. It may be true in other languages but in Java, both are very different.

Leave a Reply

Your email address will not be published. Required fields are marked *