Programming Language Constants – Quick Reference

This is a quick reference for how constants are declared and used in different programming languages.

Software developers writing programs in different languages are often challenged by lack of consistency for how constants are declared and used (pun intended!). This is a quick reference that compares rules and behavior around constants in Java, Swift, Javascript (ES6), Rust, and C++. The motivation for this is the annoying difference between the let keyword in Javascript ES6 and Swift that drives me crazy (it is a variable in ES6 and a constant in Swift).

Language“Constant” KeywordWhen is “Constantness” Enforced?Rules Related to “Constantness”
Javascript ES6constRun time1. const variables must be assigned a value when declared and cannot be reassigned
2. const does not define a constant value, it defines a constant reference to a value (you can change the properties of a const object)
SwiftletCompile time1. After the value of a let variable is set, it cannot be changed.
2. But if it is initialized with a class object, the object itself can change, but the binding between the constant name and the object it refers to can’t.
C++#defineGives compile-time warning if changedProgrammers use #define preprocessor macro’s to declare constants but these can be modified by subsequent #define statements (getting a compiler warning) or undefined with the #undef statement so there is no real enforcement of “constantness”
C++constCompile time1. A const variable can’t have its value changed
2. Pointers declared as const can’t have their value changed but the memory they point to can be changed
3. You can only call const methods on objects that are declared as const
JavaconstN/AIt is interesting that const is a reserved keyword in Java but it is not used
JavafinalCompile Time1. Once a final variable is assigned, it can’t be changed but it doesn’t need to be assigned at the point of initialization
2. If the final variable is an object, it will always point to the same object but the properties of the object can change
PythonN/AN/ANo such thing as a constant in Python. Everything can change
RustconstCompile Time1. Similar to Javascript ES6, constants are declared using the const keyword while variables are declared using the let keyword
2. Constants cannot have their value changed
3. Be aware that constants may not refer to the same memory location

JavaScript ES6

ES6 introduced two new JavaScript keywords: const and let. Both have block scope. The difference is that const variables can’t vary – they have to be assigned a value when they are declared and can’t be reassigned after that.

const PI; // will give an error

const PI = 3.14;
PI = 3.14159; // will give an error

But it is really the reference that is the constant and not the value it refers to. So you can have a const object but still reassign its properties.

const myCar = {color: "red", make: "honda", miles: 15000};
myCar.miles = 15500; // this is OK

Swift

In contrast to Javascript ES6, the let keyword in Swift is used to declare a constant, not a variable.

let (firstNumber, secondNumber) = (5, 23)

When a let constant is declared in global scope, it needs to be initialized (like the declaration above). But if you declare it inside a function, it can be initialized at runtime as long as it gets a value before it is first used.

One thing to keep in mind is that if the constant is an object, the value it takes is a reference to the object that can’t be changed, but the properties of the object CAN change.

C++

Variables declared as const can’t have their values changed.

const int maxarray = 255;

Neither can pointers but you can change the memory location that the pointer points to.

char *mybuf = 0, *yourbuf;

char *const aptr = mybuf;

*aptr = 'a'; // OK because pointer value isn't changing

aptr = yourbuf; // Error C3892

Objects that are declared const can only have const member functions called.

const Date BirthDate( 1, 18, 1953 );

BirthDate.getMonth(); // Okay if getMonth() is a const function

BirthDate.setMonth( 4 ); // C2662 Error if setMonth is not a const

Note that because in C++, const is part of the type, it can be cast away and the value can be modified.

Java

A final variable can only be assigned once.

final int i = 1;

i = i + 5; // error

But objects declared final can have their properties changes.

final StringBuffer sb = newStringBuffer("Hello");

sb.append(" Steve"); // this is OK

Rust

Rust uses the const and let keywords similar to the way Javascript ES6 uses them.

const N: i32 = 5; // can't be modified

The nuance with Rust is that constants have no fixed address in memory. This is because they’re effectively inlined to each place that they’re used analogous to #define in C++ but not exactly the same. References to the same constant are thus not necessarily guaranteed to refer to the same memory address.

Featured image of Rocks at Joshua Tree Copyright © 2020 Steve Kowalski