The Multilingual Developer — Defining Variables and Constants in 6 Programming Languages

Amr Abed
Analytics Vidhya
Published in
4 min readFeb 11, 2020

In this series, I will be documenting the differences between 6 of the most trending programming languages. The 6 selected languages are C++, Java, Python, JavaScript, Swift, and Kotlin. If you are already familiar with one or more of those languages, this series can make it easier for you to learn the other languages by seeing how it differs and matches what you already know.

Now, let’s start with the basics. Let’s begin by exploring how to define variables and constants in each language.

C++

To define a variable in C++, we use the data type followed by the variable name and optionally an initial value. For example, to define a variable named x of type int (integer) with an initial value of 5 , we use:

int x = 5;

Like in almost every language, the initial value is optional in variable definition, which means that the following definition is also correct:

int x;

The data type is required in C++ even when the variable is initialized. However, C++11 introduced the keyword auto to alleviate the need to explicitly specify the data type if it can be inferred from the initial value. For example, the above example can be rewritten using the auto keyword as:

auto x = 5;  // int data type is inferred

Note that the word auto is still required in that case. The following cannot be used for variable definitions in C++ and would result in a compilation error stating that the variable was not defined.

x = 5;

To define a constant in C++, we simply add the const keyword before the definition as follows:

const int x = 5;

Java

In Java, we define a variable the same way we do in C++:

int x = 5;

Similarly, the data type is required, but the initialization is optional.

To define a constant, however, we precede the definition with the final keyword:

final int x = 5;

Python

Unlike C++ and Java, Python does not require a data type. The data type is always inferred from the initial value. To define a variable in Python, we simply initialize it:

x = 5

Moreover, the data type of the variable can be changed between different parts of the code by assigning it to new data of a different type. The following is permissible in Python:

x = 5           # x is an integer# Some Codex = "Hello"     # x is now a string

Obviously, the initialization of the variable is required in Python since the initialization is actually the definition. Otherwise, you would just have the variable name, which would result in an error, since the variable was not defined.

On the other hand, Python does not support constants. That is, there is no way to tell the interpreter not to allow the re-assignment of a variable. To define a constant, developers rely on naming conventions to avoid accidentally changing the value of a constant by using camelCase names for variables and ALL_CAPS names for constants:

X = 5

Python 3.8 introduced typing.Final to indicate to type checkers, like mypy, that a name cannot be re-assigned. However, it does not prevent re-assignment according to the documentation:

There is no runtime checking of these properties. See PEP 591 for more details.

The following code would run normally, but an error would be reported by a type checker.

from typing import FinalX: Final = 5X = 3    # error reported by type checker

JavaScript

JavaScript has two keywords for defining a variable, namely let and var . The main difference between the two keywords is the scope of the defined variable, i.e.,let defines a block-scoped local variable. The data type of the variable is inferred from the value of the variable, but initialization is optional. Any of the following definitions is acceptable.

let x;let x = 5;var x;var x = 5;

Similar to Python, the data type of the variable can be changed by re-assigning the variable to a value of a different type.

x = 5           // x is an integer/*  Some Code  */x = "Hello"    // x is now a string

To define a constant, we use the const keyword and, of course, the initialization is required in this case.

const x = 5;

Swift

In Swift, the var keyword is used to define a variable. If the variable is initialized, then the data type is optional and can be inferred from the value.

var x = 5

If the variable is not initialized, the data type must be provided as follows:

var x: Double

To define a constant, we use the let keyword:

let y = 4

Kotlin

Last but not least, Kotlin uses the keywords var and val for defining variables and constants respectively.

var x = 5     // variableval y = 3     // constant

Similar to Swift, the data type is optional if the variable is initialized, but required otherwise:

var x: Int

Summary

The table below summarizes the comparison between the 6 languages when it comes to defining variables and constants.

Variable and Constant Definition in the 6 Languages

--

--

Amr Abed
Analytics Vidhya

Computer Engineer with passion for learning and knowledge sharing