PHP
You can declare constants in PHP using the define keyword.
define("name", value);
The following examples show how to declare constants in PHP.
define("VAT", 0.22); define("COMPUTER_NAME", "pc01"); define("FAVORITE_SONG", "We are the world");
Notice: Please note that PHP requires that all statements be terminated with a semicolon ( ; ).
Once a constant is defined, its value cannot be changed while the program is running.
Java
You can declare constants in Java using the sequence of keywords static and final.
static final type name = value;
The following examples show how to declare constants in Java.
static final double VAT = 0.22; static final int NUMBER_OF_PLAYERS = 25; static final String COMPUTER_NAME = "pc01"; static final String FAVORITE_SONG = "We are the world"; static final char FAVORITE_CHARACTER = ' w';
Notice: Please note that Java requires that all statements be terminated with a semicolon ( ; ).
Notice: Please note that in Java you assign a value to a constant of type String using double quotes (” “), but you assign a value to a constant of type char using single quotes (‘ ‘).
Once a constant is defined, its value cannot be changed while the program is running.
C++
You can declare constants in C++ using the keyword const.
const type name = value;
The following examples show how to declare constants in C++.
const double VAT = 0.22; const int NUMBER_OF_PLAYERS = 25; const string FAVORITE_SONG = "We are the world"; const char FAVORITE_CHARACTER = 'w';
Notice: Please note that C++ requires that all statements be terminated with a semicolon ( ; ).
Once a constant is defined, its value cannot be changed while the program is running.
C#
You can declare constants in C# using the keyword const.
const type name = value;
The following examples show how to declare constants in C#.
const double VAT = 0.22; const int NUMBER_OF_PLAYERS = 25; const string FAVORITE_SONG = "We are the world"; const char FAVORITE_CHARACTER = 'w';
Notice: Please note that C# requires that all statements be terminated with a semicolon ( ; ).
Once a constant is defined, its value cannot be changed while the program is running.
Visual Basic
You can declare constants in Visual Basic using the keyword Const.
Const name = value
The following examples show how to declare constants in Visual Basic.
Const VAT = 0.22 Const NUMBER_OF_PLAYERS = 25 Const FAVORITE_SONG = "We are the world" Const FAVORITE_CHARACTER = "w"
Once a constant is defined, its value cannot be changed while the program is running.
Python
You must accept it! You cannot declare constants in Python, there is no such option! However, you can still use variables (in uppercase) instead and take care not to alter their initial values each time you use these variables in your program.
The following example shows how to declare variables (used as constants) in Python.
VAT = 0.22 NUMBER_OF_PLAYERS = 25 FAVORITE_SONG = "We are the world" FAVORITE_CHARACTER = "w"
Remember! In computer programming, once a constant is defined, its value cannot be changed while the program is running. Unfortunately, this is not true for Python. Since you use a variable in place of a constant, you must be careful not to accidentally alter their initial values below in your programs.