Variables are a very important part of programming. We define variables in order to store data in simple or complex forms. Constant is used to create a variable which value can not be changed. This may seem a bit strange but in some cases, it can be very helpful.
变量是编程中非常重要的部分。 我们定义变量以便以简单或复杂形式存储数据。 常量用于创建一个不能更改值的变量。 这似乎有些奇怪,但在某些情况下可能会很有帮助。
常数变数 (Constant Variable)
In general constant variable definition is very similar to the normal variable definition in most of the programming languages. There are some exceptions where we will learn them below. Normally variables values may change without any restriction. But in some cases like preventing to change data or damage, we may need to protect variable content or data from outside effects and changes. The general syntax of the constant is like below.
通常,在大多数编程语言中,常量变量定义与普通变量定义非常相似。 在某些例外情况下,我们将在下面进行介绍。 通常,变量值可以无限制地更改。 但是在某些情况下,例如防止更改数据或损坏,我们可能需要保护可变内容或数据免受外部影响和更改。 常量的一般语法如下。
const TYPE VARIABLE_NAME;
const TYPE VARIABLE_NAME;
- `const` is the keyword which is generally used to set constant the given variable const是通常用于将给定变量设置为常数的关键字
- `TYPE` is the variable type like integer, string, character, etc.TYPE是变量类型,例如整数,字符串,字符等。
- `VARIABLE_NAME`is the name of the variable which will be used as constant. “ VARIABLE_NAME”是将用作常量的变量的名称。
常量变量规则 (Constant Variable Rules)
There are some rules while using constant variables.
使用常量变量时有一些规则。
- Constant array elements can be changed in JavaScript where new elements can be assigned or existing elements can be removed. 可以在JavaScript中更改常量数组元素,在JavaScript中可以分配新元素或删除现有元素。
JavaScript常数变数 (JavaScript Constant Variable)
JavaScript programming language provides the const
keyword before the variable definition. But while defining the variable we will not use let
or var
keywords. In complex objects the object content can change one by one but can not be changed the object completely.
JavaScript编程语言在变量定义之前提供const
关键字。 但是在定义变量时,我们将不使用let
或var
关键字。 在复杂的对象中,对象内容可以一一更改,但不能完全更改对象。
const age= 25;
const name ="poftut.com";
const myobject = {"poftut.com",25};
C / C ++常数变数 (C/C++ Constant Variable)
C/C++ programming languages use the const
keyword in order to define constant. The variable which is defined as constant will be stored in the .rodata
segment of the application. Below we define different types of constant.
C / C ++编程语言使用const
关键字来定义常量。 定义为常量的变量将存储在应用程序的.rodata
段中。 下面我们定义常数的不同类型。
const int age = 25;
const char* name[] = "poftut.com";
const long range = 1223235234;
PHP常量变量 (PHP Constant Variable)
PHP also provides the define()
function in order to create constant variables. PHP constant definition process is a bit different where we will provide the constant variable name and value as parameters to the define() function
PHP还提供了define()
函数以创建常量变量。 PHP常量定义过程有些不同,我们将为constant()函数提供常量变量名称和值作为参数。
define("age" , "25");
define("name" , "poftut.com");
It may seem that defined constants like age and name are both string but they are not. They are constant and do not have any type where every time they are used they will act according to usage position.
似乎年龄和名称之类的已定义常量都是字符串,但不是。 它们是常数,并且没有任何类型,每次使用它们时,它们都会根据使用位置进行操作。
Java常量变量 (Java Constant Variable)
Java programming language also provides constant variables. In order to make a variable or object constant static
and final
keyword is used. There are also other functions of the static keyword. Below we can see some constant variable examples.
Java编程语言还提供了常量变量。 为了使变量或对象恒定static
,使用了static
和final
关键字。 static关键字还有其他功能。 下面我们可以看到一些常量变量示例。
static final int age=25;
static final String name = "poftut.com";
翻译自: https://www.poftut.com/what-is-constant-variable-in-programming/