var george = 1; george = "Hi";
Most programming languages, like C++ and Java, are "typed" languages. In a typed language, a given variable may only ever be ONE data type. For instance, in
int george = 1; string fred = "Hi"; float ethel = 1.052;
In the above example in C/C++, I have declared and initialized three different variables. The variable george is an integer (int) and may only ever contain an integer. The variable fred is a string, and may only ever contain a string. The variable ethel is a float, and may only ever contain a number with decimal places. In typed languages like C/C++, variable data types remain fixed for the duration of a program.
var kate = "hi"; var louie = 10.25; var josie = louie; var sam = 200; var answer = louie + josie + sam; kate = answer;
In order of the examples: the variable kate now contains a string value, "hi". The variable louie now contains a number. The variable josie has been set equal to louie, which means that a copy of louie's contents have been placed inside of josie; both josie and louie, then, are equal to 10.25. The variable sam now contains a number as well. The variable called answer has been set to the sum of louie and josie and sam, which would be 10.25 + 10.25 + 200, or the number 220.5. Note: answer is just a random name, picked because it makes sense in English; I could have named it anything. The variable kate (which has already been declared), then, has had its string content of "hi" replaced with the numerical content of the variable answer, which changes kate's data type from a string to a number.
The following example is WRONG:
louie + josie + sam = answer;
var answer = louie + josie + sam;
The following example is ALSO WRONG:
var answer = louie + JOSIE + Sam;
Copyright © 2001 Michael Masumoto. All Rights Reserved.