In programming, variables are used to store information or data.
You can think of these as a bucket, container or anything that is used to store something in it.
Just like real life where sometime we name different containers as per things stored in them. For example, vegetable container, fruit container, lunch box, etc. In programming, variables also have name. We name variable to help our program to easily refer information, that have been stored in a variable, anywhere and anytime during its execution.
As we can see many different type of storage options in our daily life. Same is true for variables in programming language. Variables are of three basic types.
As the name suggest String variables are used to store strings or text information. For example, a variable ‘name’ can be used to store name information, and a variable ‘address’ can be used to store address information. Similarly, numeric variables are used to store different type of numeric data. Class variables (or Objects) are special type of variables that store of represent Objects of a Class. We will discuss ‘Classes and Objects’ later in another course.
Therefor, in nutshell a variable consists of three basic parts.
And that’s why in different programming languages there are different ways to declare or name a variable. But in plain English we can declare or name a variable like this.
I have a Glass Bottle of Water.
OR
I have {Type} {Name} of {Thing or Object}.
So if we remove different English punctuation words from above line, we will left with this.
{Type} {Name} {Value}
So in any programming language a variable always or should be consist of these three key parts.
Here are some variable declarations and initialization from different programming languages.
// Java
String name = "John Doe";
// C++
string name = "John Doe";
// C#
string name = "John Doe";
Please note variable declaration and initialization are different. But, most of the time we declare and initialize variable on same line, as we declared and initialized our variables above in same line.
// A variable declaration will
// look like this.
String name;
int age;
// Note we don't set or provide
// any value of these variables.
// Now as we have already declared
// our variables above. Let's
// initialize those now
name = "John Doe";
age = 24;
// Note: 'int' is a way to declare variable
// of data type number without
// decimals points.
Each programming language has it’s own way of variable declaration. Discussing a specific programming language is out of our scope of this course. That’s why I will not go into details for now. If you want to know more about variable declaration in a programming language of your choice, I would encourage you to do some search for this.
But most of the languages do have one thing in common and that is variable naming convention. Most of the languages support camelCase notation. Therefore, variable naming can be similar in different programming languages. For example.
// Single word variable should all lower case.
string name = "John Deo";
int age = 24;
// Variable names that consist of two or more words
string personName = "John Doe";
int houseNumber = 123;
You should have basic understanding of variable and variable declarations by now.
Let’s move on to next chapter of this course. Please click on “Next” to continue.