Variables and Data type
Variable declairation - int password = 1745
Here password is a variable who store a data(1745) of integer type.
Remember three points when creating a variable name.
- Variable name should be alphanumerical.
- Variable name can contain underscore_ .
- Varaible name shouldn't be started with a number.
(Recommonded cases for variable names - loswecase(myvar), snake case(my_name), camel case(myName) )
Data Type
There are various data types of variables, each of them can store different kind of data and have different operations. Common variable types are -
- Primitive Types
- Composite Types
- Reference Types
- Dynamic Types
1. Primitive Types
These are the simplest data types that hold a single value. They include:
- Integer (int) : Stores whole numbers, both positive and negative.
example ➼ 3 -8 - Double/Real/Floating Point (float or double) : Holds real numbers, i.e., numbers with a decimal point.
example ➼ 1.5 - Boolean (bool) : Represents true/false values.
example ➼ 1 false - Character (char) : Stores a single character.
example ➼ 'A' 'c' '8' - String : A sequence of characters, often used to represent text.
example ➼ "Hello" "J8hn1"
2. Composite Types
These types can hold multiple values :
- Array : A collection of elements of the same type. ➼ [12,34,5,6]
- Structures (struct) : Custom data types that group together variables of different types.
- Unions Similar to structs but can store different types of data in the same memory space.
- Enumerations (enum) : Define a type that consists of a set of named constants.
3. Reference Types
These types hold references to other data types or objects.
- Pointers : Store the memory address of a variable.
- Objects : Instances of classes or custom types that can contain data and methods.
4. Dynamic Types
Languages that support dynamic typing allow variables to hold values of any type without prior declaration.
Some dynamically typed languages are - Python, JavaScript, Ruby, PHP, Perl and many more...