Syntax of Variable Declaration

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   hear noise   go to next page

Answer:

No.

Syntax of Variable Declaration

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 as long as you follow the pattern.

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

QUESTION 5:

Is the following correct?

int answer;
double rate = 0.05;