Besides printing out a pretty Xmas tree, a program can compute many things. To store values for computations, we need variables. Because there are different types of values such as integers, decimals or pure texts, variables have different types to match the types of values that they save. This is why when "creating" a new variable, we need to declare the type of this variable and give it an initial value.
B. Let's turn the Phone Charge algorithm in 1.5.1 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.
C. Try: there are 8 primitive types in Java: int, short, long, byte, float, double, char, boolean . Their descriptions are below. Can you declare a variable of each type and give initial values?