# Variables & Data

A variable holds information, numbers, words: data. It gives the programmer the ability to store, manipulate, retrieve, and output information.\
\
Some programming languages are ["Typed" languages](https://hackernoon.com/i-finally-understand-static-vs-dynamic-typing-and-you-will-too-ad0c2bd0acc7). They have a specific *kind* of variable, depending on what you want to do. This is important because different types of variables require different amounts of memory (RAM) in the computer. C++ and Java are strongly (or statically) typed languages. \
\
Here are a few variable types, the amount of memory they require, and their limitations:

| DATA TYPE         | CONTENTS                          | SIZE (IN BYTES) | RANGE                                            |
| ----------------- | --------------------------------- | --------------- | ------------------------------------------------ |
| short int         | Whole Numbers                     | 2               | -32,768 to 32,767                                |
| int               | Whole Numbers                     | 4               | -2,147,483,648 to 2,147,483,647                  |
| unsigned int      | Positive Whole Numbers            | 4               | 0 to 2,147,483,647                               |
| char              | Single Character                  | 1               | -128 to 127                                      |
| float             | Numbers, including decimal values | 4               | $$17\times10^{-38} \text{ to }1.7\times10^{38}$$ |
| bool (or boolean) | True or False                     | 1               | 0 to 1                                           |
| String            | Text\*                            | Varies          | Char\[] array                                    |

*\*Strings are not a primitive data type, they are an array of chars or a special type called an **object**.*\
&#x20;   More on data types:\
&#x20;         **C++:** <https://www.geeksforgeeks.org/c-data-types/>\
&#x20;         **Java:** <https://www.geeksforgeeks.org/data-types-in-java/>\
&#x20;         **Javascript:**  <https://www.geeksforgeeks.org/variables-datatypes-javascript/>

{% hint style="info" %}
When creating a variable in *strongly typed* languages, the programmer needs to know exactly what the variable will be holding. The micro-management of memory and data types is what makes C++ such a strong language for things like video games.
{% endhint %}

**Javascript** is [not a strongly typed language](http://exploringjs.com/impatient-js/ch_values.html#whats-a-type). Neither is Python, Ruby, Perl, or Lua. They are what we call [*dynamically typed*](https://stackoverflow.com/questions/1517582/what-is-the-difference-between-statically-typed-and-dynamically-typed-languages).  They can transition between variable types and do not require the programmer to declare what type of variable is required. The interpreter will even convert a character to a number and back again, as needed.

{% tabs %}
{% tab title="Javascript" %}
Javascript uses a keyword to create (declare) a variable. \
There are three different keywords, each with a special meaning:

```javascript
// can hold all sorts of data and is globally scoped (more on scope later)
var myVariable;

// can hold all sorts of data and is locally or block scoped
let myOtherVariable;

// can hold all sorts of data but that data cannot change (or shouldn't, at least)
const myConstant;
```

***For this course, it would be best if we use*** `let` ***as often as possible.***

I don't expect everyone to read it, but [here is a great chapter](http://eloquentjavascript.net/01_values.html) on variables in JS.

{% hint style="warning" %}
The "const" declaration in JS does not necessarily mean the variable cannot change. This is a strange property of Javascript and is above the scope of this course. If you'd like to learn more, you can search "javascript immutable const".
{% endhint %}
{% endtab %}

{% tab title="Python" %}
Python is a dynamically typed language. You don't even need to put a declaration at all before a variable. You can simply give a new variable name and Python will open memory for it.

```python
a = 123 			            # integer
b = 123L			            # long integer
pi = 3.14 			            # double float
myString = "hello" 			    # string
myList = [0,1,2] 			    # list
myTuple = (0,1,2) 			    # tuple
myFile = open(‘hello.py’, ‘r’) 	# file
```

{% endtab %}

{% tab title="C++" %}
C++ is a very strongly or statically typed language. It even has something called *pointers* which point to (reference) an area in memory instead of a value. Kind of like pointing to your locker instead of seeing what is in your locker.

{% code title="" %}

```cpp
int myInt;
int * myPointer;   // A pointer to an int
float myBigNumber;
bool theSkyIsBlue = true;   // You can initialize variables as you declare them
char aSingleLetter;
char myWord[] = { 'H', 'e', 'l', 'l', 'o', '\0' };    // String
char myOtherWord[] = "Hello";
```

{% endcode %}

{% endtab %}

{% tab title="Java" %}
Java is extremely similar to C++ in terms of variable declarations and options. It is a strongly (or statically) typed language.

```java
int numberOfDays = 7;
byte nextInStream;
short hour;
long totalNumberOfStars;
float reactionTime;
double itemPrice;
String helloWorld = "Hello World!";
String newStringOnHeap = new String("Goodbye Sleep!");
```

{% hint style="info" %}
Java is a bit different than C++ in that it has a **`String`** variable (array of chars). \
A String is **more** than just a variable - it is something called an **`Object`**. You'll see in the code above, there are two ways to declare a String. That's because there is something called a **`Heap`** in Java. More on that in Grade 12!
{% endhint %}
{% endtab %}
{% endtabs %}
