User Tools

Site Tools


cpp_const_keyword

CPP const keyword

Return to const, C++ Reserved words, C++, Reserved Words, CPP Glossary, CPP Topics


Let's explore the `const` keyword in C++ and compare its equivalent or closest concept in Python, Java, C#, Kotlin, JavaScript, TypeScript, PHP, Go, Rust, Swift, Transact-SQL, and PL/SQL. The `const` keyword in C++ is used to define variables whose value cannot be changed after initialization.

C++ `const`

In C++, `const` is used to declare variables or objects as constant, meaning their value cannot be changed once they are initialized. It can also be used with pointers, indicating either that the pointed-to value is constant, the pointer itself is constant, or both.

```cpp const int x = 10; // x cannot be changed ```

[C++ Documentation](https://en.cppreference.com/w/cpp/language/cv)

Python Equivalent

Python does not have a built-in `const` keyword. However, the convention is to use all-uppercase letters to indicate that a variable should not be changed.

```python X = 10 # Conventionally constant, but not enforced by Python ```

[Python Documentation](https://www.python.org/dev/peps/pep-0008/#constants)

Java `final`

Java uses `final` as its equivalent to C++'s `const`. A `final` variable can only be initialized once, either at the declaration or in the constructor.

```java final int x = 10; ```

[Java Documentation](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html)

C# `const`

C# uses `const` similarly to C++. Constants are evaluated at compile time and must be initialized as they are declared.

```csharp const int x = 10; ```

[C# Documentation](https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/constants)

Kotlin `val`

Kotlin uses `val` to declare a read-only property or local variable. Once initialized, its value cannot be changed.

```kotlin val x = 10 ```

[Kotlin Documentation](https://kotlinlang.org/docs/properties.html#read-only-properties)

JavaScript `const`

JavaScript's `const` declares a block-scoped variable that cannot be reassigned. However, the contents of a `const` object can be altered.

```javascript const x = 10; ```

[JavaScript Documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const)

TypeScript `const`

TypeScript, extending JavaScript, uses `const` in the same manner, for defining variables that cannot be reassigned.

```typescript const x: number = 10; ```

[TypeScript Documentation](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#the-primitives-string-number-and-boolean)

PHP `define()` or `const`

In PHP, constants can be defined using the `define()` function or the `const` keyword. They are immutable once set.

```php define(“X”, 10); // Using define() const Y = 10; // Using const keyword ```

[PHP Documentation](https://www.php.net/manual/en/language.constants.php)

Go `const`

Go uses `const` to declare a constant value. A `const` in Go can be character, string, boolean, or numeric value.

```go const x int = 10 ```

[Go Documentation](https://golang.org/ref/spec#Constants)

Rust `const`

Rust's `const` keyword is used for constants that are evaluated at compile time. Rust also has `let` for immutable variables, but `const` is used for true constants.

```rust const X: i32 = 10; ```

[Rust Documentation](https://doc.rust-lang.org/reference/items/constant-items.html)

Swift `let`

Swift uses `let` to declare a constant. Once set, its value cannot be changed.

```swift let x = 10 ```

[Swift Documentation](https://docs.swift.org/swift-book/LanguageGuide/TheBasics.html#ID310)

Transact-SQL `CONSTANT`

Transact-SQL allows for the definition of constants in the context of creating functions or procedures, typically using `DECLARE` alongside `CONSTANT`.

```sql DECLARE @x AS INT = 10 ```

[Transact-SQL Documentation](https://docs.microsoft.com/en-us/sql/t-sql/language-elements/declare-local-variable-transact-sql)

PL/SQL `CONSTANT`

PL/SQL uses `CONSTANT` to define a constant. A constant must be initialized in its declaration.

```plsql x CONSTANT NUMBER := 10; ```

[PL/SQL Documentation](https://docs.oracle.com/database/121/LNPLS/constants-and-variables.htm#

LNPLS013)

Each of these languages has its own way of handling immutable values, with various keywords and conventions serving the purpose of C++'s `const`. While the implementation and exact semantics may vary, the underlying concept of defining values that should not change remains consistent across languages.


Fair Use Sources

C++: C++ Fundamentals, C++ Inventor - C++ Language Designer: Bjarne Stroustrup in 1985; C++ Keywords, C++ Built-In Data Types, C++ Data Structures (CPP Containers) - C++ Algorithms, C++ Syntax, C++ OOP - C++ Design Patterns, Clean C++ - C++ Style Guide, C++ Best Practices ( C++ Core Guidelines (CG)) - C++ BDD, C++ Standards ( C++ 23, C++ 20, C++ 17, C++ 14, C++ 11, C++ 03, C++ 98), Bjarne Stroustrup's C++ Glossary, CppReference.com, CPlusPlus.com, ISOcpp.org, C++ Compilers (Compiler Explorer, MinGW), C++ IDEs, C++ Development Tools, C++ Linter, C++ Debugging, C++ Modules ( C++20), C++ Packages, C++ Package Manager ( Conan - the C/C++ Package Manager), C++ Standard Library, C++ Libraries, C++ Frameworks, C++ DevOps - C++ SRE, C++ CI/CD ( C++ Build Pipeline), C++ Data Science - C++ DataOps, C++ Machine Learning, C++ Deep Learning, Functional C++, C++ Concurrency, C++ History, C++ Topics, C++ Bibliography, Manning C++ Series, C++ Courses, CppCon, C++ Research, C++ GitHub, Written in C++, C++ Popularity, C++ Awesome , C++ Versions. (navbar_cplusplus – see also navbar_cpp_containers, navbar_cppcon, navbar_cpp_core_guidelines, navbar_cpp23, navbar_cpp20, navbar_cpp17, navbar_cpp14, navbar_cpp11)

Reserved Words: Programming Language Keywords, aka Reserved Identifiers. (navbar_reserved_words - see also navbar_programming)


© 1994 - 2024 Cloud Monk Losang Jinpa or Fair Use. Disclaimers

SYI LU SENG E MU CHYWE YE. NAN. WEI LA YE. WEI LA YE. SA WA HE.


cpp_const_keyword.txt · Last modified: 2024/04/28 03:13 (external edit)