Other than computing with literals, a program often stores values for output or further computations. We store values in variables. Because there are different types of values such as int, double or strings, variables have different types to match the types of values that they store. This is why when "creating" a new variable, we need to declare the type of this variable and sometimes, give it an initial value.
B. Let's turn the Phone Charge algorithm in 1.6 into a program. First we need to declare and initialize three variables:
The above declares 3 variables and immediately give them initial values. This is recommended because it is concise and clear. In the case that you do not know the initial values, you can declare variables first and initialize them later.
Keep in mind that a variable can be declared ONLY ONCE. It can be set to new values as often as needed.
There are 8 primitive types in Java: int, short, long, byte, float, double, char, boolean . Their descriptions below might help you to answer R4.27 and R4.28 above. Can you declare a variable of each type and give proper initial values?