Variables and Data Types
Understanding variables and data types is fundamental to programming. This section explores how data is represented and manipulated in code.
What are Variables?
A variable is a named storage location in a computer's memory that holds a value. This value can be changed during the execution of a program. Variables allow us to store, retrieve, and manipulate data efficiently.
Variable Declaration and Assignment
In most programming languages, you first declare a variable and then assign a value to it. The syntax varies, but the concept remains the same.
// C# Example
int age; // Declaration
age = 30; // Assignment
string name = "Alice"; // Declaration and Assignment
// JavaScript Example
let score;
score = 100;
const PI = 3.14159; // Constant declaration
What are Data Types?
A data type specifies the type of value that a variable can hold and the operations that can be performed on it. Data types help the compiler or interpreter understand how to interpret the data and allocate memory appropriately.
Common Data Types
Here are some of the most common fundamental data types found across many programming languages:
Type | Description | Example Values |
---|---|---|
Integer (e.g., int , long ) |
Whole numbers, positive or negative, without decimal points. | 10 , -500 , 1234567890 |
Floating-Point (e.g., float , double ) |
Numbers with a decimal point, representing real numbers. | 3.14 , -0.001 , 1.23e10 |
Boolean (e.g., bool ) |
Represents one of two values: true or false . |
true , false |
Character (e.g., char ) |
A single character, enclosed in single quotes. | 'A' , 'z' , '!' |
String (e.g., string ) |
A sequence of characters, enclosed in double quotes. | "Hello, World!" , "MSDN" , "" (empty string) |
Null / None (e.g., null , None ) |
Represents the intentional absence of any value. | null , None |
Type Systems
Programming languages employ different type systems:
- Statically Typed Languages: Variable types are checked at compile time. Errors related to type mismatches are caught early. Examples: C++, Java, C#, TypeScript.
- Dynamically Typed Languages: Variable types are checked at runtime. This offers more flexibility but can lead to runtime errors. Examples: Python, JavaScript, Ruby.
Type Inference
Some modern languages can infer the type of a variable from the value assigned to it, reducing the need for explicit type declarations.
// C# with type inference (using 'var')
var message = "This is a string."; // Compiler infers 'message' is a string
var count = 100; // Compiler infers 'count' is an integer
// JavaScript uses dynamic typing, types are inferred at runtime
let quantity = 5; // quantity is a number
quantity = "five"; // quantity is now a string
Data Type Conversion (Casting)
Sometimes it's necessary to convert a value from one data type to another. This process is called type conversion or casting.
- Implicit Conversion: The compiler automatically converts a type to another (e.g., converting an
int
to adouble
). - Explicit Conversion: The programmer must explicitly tell the compiler to convert a type (e.g., converting a
double
to anint
, which might involve data loss).
Explicit Casting Example
// C# Example of explicit casting
double pi = 3.14159;
int intPi = (int)pi; // Explicitly cast double to int (truncates decimal)
Console.WriteLine(intPi); // Output: 3
string numberStr = "123";
int numberInt = int.Parse(numberStr); // Convert string to integer
Console.WriteLine(numberInt); // Output: 123
Structured Data Types
Beyond primitive types, programming languages offer ways to represent more complex data structures:
- Arrays: Ordered collections of elements of the same type.
- Objects/Structs: Collections of related data fields, often representing real-world entities.
- Lists/Collections: More dynamic and flexible ways to store groups of items.