In programming, there are a few basic data types you'll be dealing with. These include:
"Hello world!"
Numbers (can have arithmetic operations done on them, like addition, multiplication, etc.)
123
123.0
Note: Integers and floats/doubles (numbers with decimals) are always different types.
Boolean (a value that is either true or false)
true
, false
Sometimes you'll type these out directly, such as when comparing, but normally these are bound to variables.
Variables can be seen as a name which contains a value. In higher level languages like Python, Lua, or JavaScript, you don't actually have to specify the type of the variable, and can just type them out like this:
Python:
mybool = True
mystring = "test"
myint = 2
myfloat = 2.0
(they can be named whatever you want them to be, with a few exceptions)
Lua:
xxxxxxxxxx
local mybool = true
local mystr = "test"
local myint = 2
local myfloat = 2.0
JavaScript
xxxxxxxxxx
let mybool = true
let mystr = "test"
let myint = 2
let myfloat = 2.0
However, in more advanced lower level languages, you have to start specifying what type a variable should be, and that can't change.
For example:
C:
xxxxxxxxxx
int myint = 2;
float myfloat = 2.0;
char mystr[] = "Hello world!"; // Confusing example, C's version of a string
C++:
xxxxxxxxxx
int myint = 2;
float myfloat = 2.0;
string mystr = "Hello world!";
bool mybool = true;
Java:
xxxxxxxxxx
int myint = 2;
double mydouble = 2.0;
String mystr = "Hello world!";
boolean mybool = true;
Rust:
x// i32 means 32-bit integer
// All languages use 32 bit integers by default
// Rust just requires you to be explicit about it
let myint: i32 = 2;
let myfloat: f32 = 2.0; // Same thing with f32
let mydouble: f64 = 2.0; // Doubles are 64-bit floats
let mystr: String = "Hello world!".to_string(); // Strings are complex types
// OR
let mystr: String = String::from("Hello world!");
let mybool: bool = true;
// Rust is unique in the way that it's a "low level" language that can infer your types
// Unlike Python/Lua/JavaScript, this doesn't mean it can be re-assigned to any type, just that the compiler will remember the type for you.
let mybool = true;
Arrays (sometimes called "lists" or "tables") are a collection of variables. In scripting languages like Python, Lua, and JS, they usually can contain multiple types, but in lower level languages like Java, C, or Rust, they have to be typed AND have a fixed length (however there are other classes to get around this).
xxxxxxxxxx
# Python example
mylist = [1, 2, 3, 4, 5]
print(mylist[2])
# this will print "3"
# arrays start at 0, not 1
xxxxxxxxxx
// C example
int myarr[] = {1, 2, 3, 4, 5};
printf("%d", myarr[2]);
// this will also print "3"