Declaring Variables

A variable consists of a dual nature. It has a data type and a name.

Every variable must be given a name and a data type before it can be used. This is called declaring a variable. The syntax for declaring a variable is:

Screen Shot 2018-12-08 at 11.58.39 PM


Screen Shot 2018-12-09 at 4.00.02 PM

Note that a comma follows each identifier in the list except the last identifier, which is followed by a semicolon. By convention, the identifiers for variable names start with a lowercase letter. If the variable name consists of more than one word, then each word after the first should begin with a capital letter.

For example, these identifiers are conventional Java variable names: jewel3, specialRelativity, deathToNote, redInNovember, and xAxis.

Underscores conventionally are not used in variable names; they are reserved for the identifiers of constants, as we shall discuss in a later post.

Similarly, do not use dollar signs to begin variable names. The dollar sign is reserved for the first letter of programmatically generated variable names—that is, variable names generated by software, not people. Like with life in general, although arbitrariness may sound a disagreeable thing now, the value of following these conventions will become clearer as you gain more experience in Java and your programs become more complex.