What’s the difference between “final” and “const” in Java?

I’m following the Java tutorials at tutorialspoint.com/java and the author mentions the “final” keyword at some point in the text. This either wasn’t explained or I glossed over it, and I wondered, “what’s the difference between a ‘final’ variable and a ‘const’ (constant) variable in Java?”

After some Googling, I found the answer: “const” doesn’t do anything in Java. It’s a reserved keyword, but it serves no purpose. There was talk of adding it in later versions, but, apparently, making it backwards-compatible is impossible. Interestingly, C++ has also added (as of C++11) the “final” keyword, which acts the same as it does in Java.

Advertisements

2 thoughts on “What’s the difference between “final” and “const” in Java?

  1. I had been taught that Java specifically reserved a few C++ keywords with the intention of breaking C++ code. Among those words were const and goto. This was done in the early days of Java when many developers were still writing C++ and they wanted to make sure people were not copying/pasting C++ code into Java.

    Of course, I can’t confirm if this is really true or if it’s just a rumor, but that was the explanation I was given.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s