Variables and Assignment Statements

Read this chapter, which covers variables and arithmetic operations and order precedence in Java.

5. Syntax of Variable Declaration


Answer:

No

Syntax of Variable Deceleration

The word syntax means the grammar of a programming language. We can talk about the syntax of just a small part of a program, such as the syntax of variable declaration.

There are several ways to declare variables:

dataType   variableName;
      • This declares a variable, declares its data type, and reserves memory for it. It says nothing about what value is put in memory. (Later in these notes you will learn that in some circumstances the variable is automatically initialized, and that in other circumstances the variable is left uninitialized.)
dataType   variableName  =  initialValue ;
      • This declares a variable, declares its data type, reserves memory for it, and puts an initial value into that memory. The initial value must be of the correct data type.
dataType   variableNameOne, variableNameTwo ;
      • This declares two variables, both of the same data type, reserves memory for each, but puts nothing in any variable. You can do this with more than two variables, if you want.
dataType   variableNameOne  =  initialValueOne, 
           variableNameTwo  =  initialValueTwo ;
      • This declares two variables, both of the same data type, reserves memory, and puts an initial value in each variable. You can do this all on one line if there is room. Again, you can do this for more than two variables as long as you follow the pattern.

If you have several variables of different types, use several declaration statements. You can even use several declaration statements for several variables of the same type.


Question 4:

Is the following correct?

    int answer; double rate = 0.05;