This book is now obsolete Please use CSAwesome instead.
3.4. Naming Variables¶
While you can name your variable almost anything, there are some rules. A variable name should start with an alphabetic character (like a, b, c, etc). You can’t use any of the keywords or reserved words as variable names in Java (
double, etc). For a complete list of keywords and reserved words see http://docs.oracle.com/javase/tutorial/java/nutsandbolts/_keywords.html.
The name of the variable should match both the function of the variable and the type of the variable. A name like
score helps make your code easier to read. Do not try to be cute on the exam and name
your variables crazy things like
thisIsAReallyLongName. This makes the code very hard to understand. The free response questions are graded by readers (high school AP CS A teachers and college faculty). You want to make the readers’ job easier, not harder!
Remember that a reader is reading thousands of exams, you don’t want to make this person work harder than necessary, so use good variable names.
The convention in Java is to always start a variable name with a lower case letter and then uppercase the first letter of each additional word. Variable names can not include spaces so uppercasing the first letter of each additional word makes it easier to read the name. Uppercasing the first letter of each additional word is called camel case. Java is case sensitive so
playerscore are not the same.
Check Your Understanding
3-3-1: What is the camel case variable name for a variable that represents a shoe size?
3-3-2: What is the camel case variable name for a variable that represents the top score?
3-3-3: What is the camel case variable name for a variable that represents the last score?