Identifiers
Variable identifiers should be lowercase with each word after the first capitalized. Here are some examples:
width, hourlyWage, totalCostPerBox
Constant identifiers should be uppercase with words separated by an underscore character. Here are some examples:
PI, UNIT_COST, KILOGRAMS_PER_POUND
Identifiers can also contain digits (0..9) but don't use digits unless you have a compelling reason to do so.
Variable Declaration
A variable corresponds to a location in memory whose value is subject to change. To declare a variable, you must specify what type of data the variable is to represent and the name of the variable (its identifier).
Syntax: | type variableName; |
Example: | int quantity; |
If you want, you can give a variable its initial value at the same time it is declared.
Syntax: | type variableName = initialValue; |
Example: | int quantity = 0; |
Constant Declaration
A constant corresponds to a location in memory whose value cannot be changed. Use a declared constant if the value is needed two or more times or if naming the value would make your code more readable. The only difference between a variable declaration and a constant declaration is that a constant declaration begins with the keyword "const" and must include the value of the constant:
Syntax: | const type constantName = constantValue; |
Example: | const double PI = 3.14159265358979; |