Is the following declaration correct?
int answer; double rate = 0.05;
Yes — as long as the names
have not already been used.
The programmer picks a name for each variable in a program. Various things in a program are given names. A name chosen by a programmer is called an identifier. Here are the rules for identifiers:
Sumare different identifiers.
A reserved word is a word which has a predefined meaning in Java.
Rather than worry about the complete list of reserved words, just remember to
avoid using names that you know already mean something,
and be prepared to make a change if you accidentally use a reserved word you didn't know.
Although dollar sign is legal in identifiers, by convention it is used for special purposes. Don't use it for ordinary variables, even if the compiler lets you.
Although legal, it is unwise to start an identifier with underscore. Also, older Java compilers allowed a single underscore as an identifier, but recent ones do not.
As a matter of programming style,
a name for a variable usually starts with a lower case letter.
If a name for a variable is made of several words, capitalize each word except the first.
These conventions are not required by syntax,
but make programs easier to read.
Which of the following variable declarations are correct? (click on a declaration to verify your answer)