Variables & Data

So, you've mastered the Hello World! program and you want to move forward? Excellent. Let's talk about storing and manipulating 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. 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×1038 to 1.7×103817\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. More on data types: C++: https://www.geeksforgeeks.org/c-data-types/ Java: https://www.geeksforgeeks.org/data-types-in-java/ Javascript: https://www.geeksforgeeks.org/variables-datatypes-javascript/

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.

Javascript is not a strongly typed language. Neither is Python, Ruby, Perl, or Lua. They are what we call dynamically typed. 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.

Javascript
Python
C++
Java

Javascript uses a keyword to create (declare) a variable. There are three different keywords, each with a special meaning:

// 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 on variables in JS.

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".

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.

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

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.

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";

Java is extremely similar to C++ in terms of variable declarations and options. It is a strongly (or statically) typed language.

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

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!