type varwhere type is a Java type name, var is a Java variable name, and expr is an expression of type compatible with the type of var. The assignment statement= expr ;
int x = 5;defines the value of variable x as 5.
Finger Exercise: In the DrJava Interactions window, try
evaluating the following statements and expressions:
Did you get the answers that you expected?int x = 5; x*x double d = .000001; double dd = d*d; dd dd*dd 1. + dd 1. + dd*dd
Java variable names and type names must be identifiers. An identifier is any sequence of ``alphanumeric characters'' (letters, digits, and "_") beginning with a letter or "_"--except for the following keywords, which are reserved and may not be used as variable names or type names:
Java is case-sensitive; the variable X is distinct from the variable x. There are three kinds of variables in Java: fields, method parameters, and local variables. Fields and method parameters are discussed in detail in the next subsection. We will defer a discussion of local variables until Section 1.11.1.abstract default if private throw boolean do implements protected throws break double import public transient byte else instanceof return try case extends int short void catch final interface static volatile char finally long super while class float native switch const for new synchronized continue goto package this
Java includes all of the basic statement forms found in the C programming language expressed in essentially the same syntax. In the remainder of this monograph, we will introduce these statement forms as they are needed. Although Java accepts most C syntax, many common C constructions are considered bad style in Java.