Naming variables
You choose each variable's name. A good name says what the value means: price, laps, firstName. Java has a few rules for names:
- Letters, digits,
_and$are allowed, but a name can't start with a digit:level2is fine,2ndLevelisn't. - No spaces and no other symbols, like
!,-or.. - Words that are part of Java itself, such as
class,intorpublic, can't be names. - Capitals matter:
scoreandScoreare two different names.
When a name is made of several words, Java programmers write it in camelCase: the first word in lower case, and every following word starting with a capital: firstName, numberOfLaps, isRaining.
String firstName = "Grace";
int yearOfBirth = 1906;
System.out.println(firstName + " was born in " + yearOfBirth + ".");
Grace was born in 1906.A name that breaks the rules confuses the compiler, and its errors can look strange. String first name gives ';' expected, because Java reads first as the whole name and then doesn't know what name is doing there.