This chapter introduces two of the most important concepts of programming languages: variables and data types. A variable is a name associated with a data value; we say that the variable "stores" or "contains" the value. Variables allow us to store and manipulate data in our programs.
Just as fundamental as variables are data types. These, as the name suggests, are the types of data that our programs can manipulate. In Chapter 2, Lexical Structure, we saw that we can include numeric, string, and Boolean literals directly in our programs. This chapter provides more detail about these data types, and also introduces three new ones: functions, objects, and arrays. Later chapters of the book will provide much more detail about functions, objects, and arrays.
i = 2;
And the following line adds 3 to i and assigns the result to a new variable sum:
sum = i + 3;
Although it is often unnecessary, it is good programming style to declare variables before using them. You do this with the var keyword, like this:
var i; var sum;
You can also declare multiple variables with the same var keyword:
var i, sum;
And you can combine variable declaration with initial assignment to the variable:
var i = 2;
i = 10; i = "ten";
int i; // a declaration of an integer variable in C, C++, or Java