can an expression have one or more operands and zero or more operators . (java)
Answers
Answer:
Programs declare variables to store/remember information; they manipulate (examine and update) this information when they run. Simple variables typically store some value input by the user, or some value calculated by the program from user-inputs. When a program runs, the values in some of its variables change: thus, the value stored in a variable can vary as the program runs.
The EBNF rules for variable declaration appear below. Each declaration is a statement -a complete command to the computer- which the computer executes. We will cover many of Java's other statements in the next lecture.
primitive-type <= int | double | boolean | char
reference-type <= String | ... (we will learn others soon)
type <= primitive-type | reference-type
expression <= literal | ... (we will generalize this rule later)
variable-declarator <= identifier [=expression]
variable-declarators <= variable-declarator{,variable-declarator}
local-variable-declaration-statement <= type variable-declarators ;
Declarations are simple statements, which means that they end with a semicolon (see the last rule above). Variables are always declared with a type (e.g., a primitive type like int or a reference type like String), one or more names (an identifier that the programmmer chooses) whic can be optionally initialized to store a specified value.
The simplest form of a declaration is int sum; which declares a variable named sum to be of the type int, meaning that sum stores only int values. Again, notice the semicolon ending this statement. When a variable is declared this way, the value that it initially stores is undefined. We will have more to say about what Java does with undefined variables later; it is not a mistake to declare certain variables without initializing them.
If we want to declare a variable and at the same time initialize it, we can write something like int gamesPlayed = 0; explicitly telling Java to store zero in this variable initially.
In fact, we can declare a few variables in the same declaration: e.g., double angle, magnitude; declares two variables, both of type double and both storing unknown values. In multi-variable declarations, all the variables are declared to be of the same type -the one type that starts the declaration.
If we want to declare and initialize multiple variables in a single declaration (using the repetition in the variable-declarators EBNF rule), we must explicity specify the initial value of each variable. For example, int n = 0, sum = 0; initializes each variable to zero. WARNING: int n,sum = 0; initializes sum to zero, but leaves n uninitialized; making this mistake is common for beginning programmers. In fact, Java always executes declarations with multiple variables as a sequence of declarations of single variables. So executing int n,sum = 0; is equivalent to executing int n; then int sum = 0;, which makes this problem obvious.
Java imposes a syntax constraint on initialized variables: the type of the variable must be compatible with the type of the expression. We will discuss compatibility more, when we discuss implicit conversions; for now, assume that the two type must be the same.
So in the declaration int n = true; although the syntax is correct, the Java compiler will detect and report a syntax constraint error because true (a boolean literal) is not an int value; likewise boolean atCapacity = 0; exhibits the same error.
To be truthful, Java will in fact automatically convert an int value into a double if necessary , so double x = 1; is legal, and is treated as equivalently to double x = 1.; More obscurely, Java will automatically convert a char value into an int value (and vice-versa) if necessary. We will learn more about implicit type conversion later in this lecture.
Programmers often use line-oriented comments (here called side-bar comments) in declarations to document some interesting facet of a variable that is not captured by even a well-chosen name. For example, in the declarations statements
double tankSize; //Gallons
double mileage; //Miles/Gallon
Here the programmer has used the comments to describe the units of the quantity the variable stores. Extending the variable name to tankSizeInGallons is probably making it a bit too long. Note that for this style of declaration/comment, we declare just one variable per declaration statement. Pragmatically, most declarations declare just one variable.
Explanation: