Every computer program needs data to manipulate to make it useful. Data are values that the program uses when it is performing some mathematical calculation or simple using it as a counter for loops. Data are stored as variables in Java and we went through the way to declare variables in Java. Declaring variables ensures that the computer reserves memory storage areas for our variables. In declaring variables, the key thing to note is that every variable has to be associated with a data type, so that the computer knows how much memory space to allocate. Do read up Chapter 2 of the Johnson book to get more details of variable declaration and data types.
Some examples of variable declaration:
double myLength, myDepth;We have also seen how to perform arithmetic operations (+, -, *, /, %) in Java. Basically, arithmetic operations are evaluated the same way as in mathematics, with the exception of the '=' operator, which acts as the assignment operator. The assignment operator will take the result (value) of the evaluation on the right-hand-side and store it into the variable on its left-hand-side.
int englishMarks;
boolean isOK;
For example, in the program code:
a = 2, b = 8, c = 4;the evaluation on the RHS will result in a value of 32 and this will be stored in the variable answer.
answer = b * b - 4 * a * c;
As usual, do leave comments if you have questions.
No comments:
Post a Comment