Constants
C
C++
Common Lisp
Dart
F#
Go
Java
JavaScript
Rust
You can define constants inside and outside a function.
Constants Across Different Languages
C
const int number1 = 1; // constant outside the function
int main(void)
{
const int number2 = 2; // constant inside the function
}
C++
You can define constants inside and outside a function using brace initialization or assignment.
const int number1 {1}; // constant outside the function
int main()
{
const int number2 {2}; // constant inside the function
}
C#
Constants can be defined at the class and method level.
class program
{
const int number1 = 1; // class-level constant
static void Main(string[] args)
{
const int number2 = 2; // method-level constant
Console.WriteLine(number2);
}
}
Common Lisp
(defconstant +number1+ 1)
Dart
You can define constants inside and outside a function.
const int number1 = 1; // constant calculated at compile time
final int number2 = 2; // constant calculated at runtime
F#
In F#, values are immutable by default:
let number = 123
Go
Go offers flexible ways to declare constants, including multiple declarations and the iota identifier.
const pi float64 = 3.1415
// Multiple declaration of constants
const (
pi float64 = 3.1415
e float64 = 2.7182
)
// Alternative option
const pi, e = 3.1415, 2.7182
// Declaration of constants with automatic initialization
const (
a = 1
b // b = 1
c // c = 1
d = 3
f // f = 3
)
// Using iota identifier to initialize constants
const (
C0 = iota // iota is 0, increases with each row
C1 // increase by 1, iota is 1
C2 = iota // iota is 2
)
Java
Constants can be defined at the class and method level using the final keyword.
class Program {
final int number0 = 0; // non-static class-level constant
final static int number1 = 1; // class-level static constant
public static void main(String[] args) {
final int number2 = 2; // method-level constant
System.out.println(number1);
System.out.println(number2);
}
}
JavaScript
You can define constants inside and outside a code block (function).
const number = 123;
To create completely constant objects (whose properties cannot be changed), use the Object.freeze() function:
const person = {name: "Tom", age: 37};
Object.freeze(person);
person.name = "Bob";
console.log(person.name); // Tom - the property value has not changed
Kotlin
Constants can only be defined outside of a function using the const keyword.
const val number = 1
fun main() {
println(number)
}
Rust
You can define constants at the code block level (for example, a function) and in the global scope:
const NUMBER1: i32 = 1; // global constant
fn main() {
const NUMBER2: i32 = 2; // function-level constant
println!("NUMBER1 = {}", NUMBER1);
println!("NUMBER2 = {}", NUMBER2);
}Explore More IT Terms
#
A
- A Guide to SQL Query Formatting
- A/B testing
- Agile
- Algorithm complexity in 5 minutes
- Algorithms and Data Structures in C#
- An overview of the C # programming language
- An overview of the Python programming language
- Anaconda Python
- Android
- Android App Bundle
- Android SDK
- Angular
- Ansible
- Apache
- Apache Airflow
- Apache Kafka
- Apache Tomcat
- App Store
- AppCode
- Array-based stack
- ArrayList
- ASCII
- ASP.NET
- Assembly Language Lessons
B
C
D
- Data Analytics: applications of data analysis in companies
- Data Engineer - Who is it, what does a data engineer do, and an overview of the profession
- Data modeling: what it is, types, and process steps.
- Data preprocessing: a complete guide for beginners and professionals.
- Data structure
- Data structures
- Defining Aliases
- Defining Arrays
- Deque
- Developing a Website from Scratch
- Digital data: understand the importance of this asset for businesses.
- Doubly linked lists
E
F
H
- Handling errors and exceptions
- How to effectively organize your workflow
- How to Learn Java: Tips for Beginner Developers
- How to Learn PHP: A Beginner's Guide
- How to Use S3 Storage in Kubernetes with CSI
- HTML
- HTML and CSS: Definition, Application, and Operating Principles
- HTML and CSS. Layout from Scratch: What to Learn, Where to Learn, and How Long Will It Take?
- HTML Frame Structure
- HTML Link Formatting
I
- if..else construction
- Inheritance in Java: A Complete Guide to Principles and Implementation
- Inserting an Image
- Interactive Python Tutorial – Learn Programming from Scratch
- Interview Problem: Finding a Deleted Element in O(N)
- Interview Scare: The FizzBuzz Challenge
- Introduction to C++
- Introduction to Machine Learning
- Introduction to programming languages
- IT Specialist Resume (CV)
J
K
M
O
P
S
- SFML Graphics Library Tutorials
- SQL commands: see what they are, what the main ones are + examples
- SQL Interview Questions and Tasks
- SQL Lessons
- SQL Stored Procedures
- SQL Syntactic Sugar: The COALESCE Function
- Stack
- Start in analytics: Python or R
- Statistical analysis: importance for decision making.
- String formatting in Python
- Swift Lessons
- switch/match construct
T
W
- What are databases, and why do they need DBMS and SQL?
- What do Linux distributions consist of?
- What is .NET and what is it used for?
- What is a GPU in a computer, in simple terms?
- What is Big Data? Introduction, Types, Characteristics, and Examples
- What is Golang and what is it used for?
- What is Haskell and what is it used for?
- What is Kotlin and what is it used for?
- What is Linux? The History of Linux
- What is machine learning, and how does it work?
- What is Power BI: everything about the data analytics software
- What is the C++ programming language?
- What is the OSI Model: A Complete Explanation of the Seven Layers and Their Role in Networking
- Where to start learning the C programming language?
- Which Linux distribution should you choose? A Linux distribution overview
