Constants and Literals: When and How to Use Them
Read at your own pace. Code execution, quizzes, progress tracking, and the AI tutor are available in the interactive view with JavaScript enabled.
Constants and Literals: When and How to Use Them
In the realm of programming, constants and literals are fundamental building blocks that play a crucial role in constructing robust and reliable software applications. Understanding the distinction between these two concepts and employing them effectively is essential for aspiring software engineers.
```java
public class Main {
public static void main(String[] args) {
// Constants
final int MAX_SIZE = 100; // Maximum size for an array
final double PI = 3.14159; // Value of pi
final double GRAVITY = 9.81; // Acceleration due to gravity
// Literals
String name = "Alice"; // String literal
int age = 30; // Numeric literal
boolean isActive = true; // Boolean literal
// Driver code to demonstrate the usage of constants and literals
System.out.println("Constants:");
System.out.println("Maximum size: " + MAX_SIZE);
System.out.println("Value of pi: " + PI);
System.out.println("Acceleration due to gravity: " + GRAVITY);
System.out.println("\nLiterals:");
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Is active: " + isActive);
}
}
```
```javascript
// Constants
const MAX_SIZE = 100; // Maximum size for an array
const PI = 3.14159; // Value of pi
const GRAVITY = 9.81; // Acceleration due to gravity
// Literals
let name = "Alice"; // String literal
let age = 30; // Numeric literal
let isActive = true; // Boolean literal
// Driver code to demonstrate the usage of constants and literals
console.log("Constants:");
console.log("Maximum size:", MAX_SIZE);
console.log("Value of pi:", PI);
console.log("Acceleration due to gravity:", GRAVITY);
console.log("\nLiterals:");
console.log("Name:", name);
console.log("Age:", age);
console.log("Is active:", isActive);
```
```cpp
#include <iostream>
using namespace std;
int main() {
// Constants
const int MAX_SIZE = 100; // Maximum size for an array
const double PI = 3.14159; // Value of pi
const double GRAVITY = 9.81; // Acceleration due to gravity
// Literals
string name = "Alice"; // String literal
int age = 30; // Numeric literal
bool isActive = true; // Boolean literal
// Driver code to demonstrate the usage of constants and literals
cout << "Constants:" << endl;
cout << "Maximum size: " << MAX_SIZE << endl;
cout << "Value of pi: " << PI << endl;
cout << "Acceleration due to gravity: " << GRAVITY << endl;
cout << "\nLiterals:" << endl;
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
cout << "Is active: " << isActive << endl;
return 0;
}
```
```csharp
using System;
class Program {
static void Main() {
// Constants
const int MAX_SIZE = 100; // Maximum size for an array
const double PI = 3.14159; // Value of pi
const double GRAVITY = 9.81; // Acceleration due to gravity
// Literals
string name = "Alice"; // String literal
int age = 30; // Numeric literal
bool isActive = true; // Boolean literal
// Driver code to demonstrate the usage of constants and literals
Console.WriteLine("Constants:");
Console.WriteLine("Maximum size: " + MAX_SIZE);
Console.WriteLine("Value of pi: " + PI);
Console.WriteLine("Acceleration due to gravity: " + GRAVITY);
Console.WriteLine("\nLiterals:");
Console.WriteLine("Name: " + name);
Console.WriteLine("Age: " + age);
Console.WriteLine("Is active: " + isActive);
}
}
```
```go
package main
import (
"fmt"
)
func main() {
// Constants
const MAX_SIZE = 100 // Maximum size for an array
const PI = 3.14159 // Value of pi
const GRAVITY = 9.81 // Acceleration due to gravity
// Literals
name := "Alice" // String literal
age := 30 // Numeric literal
isActive := true // Boolean literal
// Driver code to demonstrate the usage of constants and literals
fmt.Println("Constants:")
fmt.Println("Maximum size:", MAX_SIZE)
fmt.Println("
Value of pi:", PI)
fmt.Println("Acceleration due to gravity:", GRAVITY)
fmt.Println("\nLiterals:")
fmt.Println("Name:", name)
fmt.Println("Age:", age)
fmt.Println("Is active:", isActive)
}
```
```py
# Sandbox code for experimenting with constants and literals
# Constants
MAX_SIZE = 100 # Maximum size for an array
PI = 3.14159 # Value of pi
GRAVITY = 9.81 # Acceleration due to gravity
# Literals
name = "Alice" # String literal
age = 30 # Numeric literal
is_active = True # Boolean literal
# Driver code to demonstrate the usage of constants and literals
print("Constants:")
print("Maximum size:", MAX_SIZE)
print("Value of pi:", PI)
print("Acceleration due to gravity:", GRAVITY)
print("\nLiterals:")
print("Name:", name)
print("Age:", age)
print("Is active:", is_active)
```
CONTINUE WITH ALGODAILY PREMIUM
Keep going with the complete tutorial.
Unlock the remaining explanations and exercises, alongside the full learning library.
Explore Premium →