>_ Der dritte Artikel aus dem CompareBuch.
Wie werden Konstanten erstellt und wie werden ihnen Werte zugewiesen?
Genauso wie kaum ein größeres Programm ohne Variablen auskommt, kommt es auch nicht ohne Konstanten aus, eben unveränderliche „Variablen“.
In diesem Artikel werden wir Konstanten erstellen, und ihnen Werte von verschiedenen Datentypen zuweisen.
PI = 3.14159 # float MAX_COUNT = 100 # int GREETING = "Hello" # str IS_ACTIVE = True # bool BYTES = b'\xde\xad\xbe\xef' # bytes TUPLE_CONST = (1, 2, 3) # tuple
const double pi = 3.14159; const int maxCount = 100; const String greeting = "Hello"; const bool isActive = true; const List<int> numbers = [1, 2, 3]; // For non-const initialization at runtime late final DateTime now; void init() { now = DateTime.now(); }
package main import "fmt" const PI float64 = 3.14159 const MAX_COUNT int = 100 const GREETING string = "Hello" const IS_ACTIVE = true // type inferred const BYTE_VAL byte = 0xFF const RUNE_VAL rune = 'A' func main() { fmt.Println(PI, MAX_COUNT, GREETING, IS_ACTIVE, BYTE_VAL, RUNE_VAL) }
const PI: f64 = 3.14159; const MAX_COUNT: i32 = 100; const GREETING: &str = "Hello"; const IS_ACTIVE: bool = true; const BYTE_VALUE: u8 = 0xff; static MY_STATIC: i64 = 42; // also immutable
const val PI: Double = 3.14159 const val MAX_COUNT: Int = 100 const val GREETING: String = "Hello" const val IS_ACTIVE: Boolean = true const val BYTE_VAL: Byte = 0x7F const val CHAR_CONST: Char = 'A' // For late init of constants (val, not const) val currentTime: Long by lazy { System.currentTimeMillis() }
#include <stdio.h> #define PI 3.14159 const int maxCount = 100; const char* greeting = "Hello"; const _Bool isActive = 1; const unsigned char byteVal = 255; int main() { printf("%f %d %s %d %u\n", PI, maxCount, greeting, isActive, byteVal); }
#include <iostream> constexpr double PI = 3.14159; const int maxCount = 100; const std::string greeting = "Hello"; const bool isActive = true; const char charVal = 'A'; int main() { std::cout << PI << " " << maxCount << " " << greeting << " " << isActive << " " << charVal << "\n"; }
program ConstantsDemo; const PI: Real = 3.14159; MAX_COUNT: Integer = 100; GREETING: String = 'Hello, World!'; IS_ACTIVE: Boolean = True; CHAR_VAL: Char = 'A'; BYTE_VAL: Byte = 255; begin WriteLn('PI: ', PI); WriteLn('MAX_COUNT: ', MAX_COUNT); WriteLn('GREETING: ', GREETING); WriteLn('IS_ACTIVE: ', IS_ACTIVE); WriteLn('CHAR_VAL: ', CHAR_VAL); WriteLn('BYTE_VAL: ', BYTE_VAL); end.
<?php define("PI", 3.14159); const MAX_COUNT = 100; const GREETING = "Hello"; const IS_ACTIVE = true; const BYTE_VAL = "\xff"; echo PI, " ", MAX_COUNT, " ", GREETING, " ", IS_ACTIVE, " ", BYTE_VAL; ?>
IDENTIFICATION DIVISION. PROGRAM-ID. ConstantsDemo. DATA DIVISION. WORKING-STORAGE SECTION. 01 PI CONSTANT VALUE 3.14159. 01 MAX-COUNT CONSTANT VALUE 100. 01 GREETING CONSTANT VALUE "HELLO". 01 IS-ACTIVE CONSTANT VALUE TRUE. 01 BYTE-VALUE CONSTANT VALUE X"FF". PROCEDURE DIVISION. DISPLAY GREETING. STOP RUN.
section .data pi dq 3.14159 maxCount dd 100 greeting db "Hello", 0 isActive db 1 byteVal db 0FFh section .text global _start _start: ; Keine echte Anzeige der Variablen, Assembler muss OS-Aufrufe für I/O verwenden. ; Dies ist nur die Deklaration und Initialisierung. mov eax, 1 ; exit xor ebx, ebx int 0x80
#!/bin/bash readonly PI=3.14159 readonly MAX_COUNT=100 readonly GREETING="Hello" readonly IS_ACTIVE=true readonly BYTE_VAL=$'\xFF' echo "$PI $MAX_COUNT $GREETING $IS_ACTIVE $BYTE_VAL"