Variables
Declaration vs. initialisation
int x;
int x = 2;
In Java one does not need to immediately specify the value of a primitive type variable. We thus distinguish between declaring a variable and initialising it. When we declare a variable we simply write its (primitive) type and its name without giving it any initial value. This will allocate the required amount of memory for each primitive type variable.
We have seen how long and double require 64 bits of memory, int and float require 32 bits of memory, char requires 16 bits and boolean requires 1 bit. You will see later on how most computers do not support the allocation of a singular bit of memory. This is not important to us.
To the left you can see on the top a declaration and below it an initialisation. After a value has been initialised we call all future writes to the variable assignments.
Variables
Declaration vs. initialisation
int x;
int x = 2;
In Java one does not need to immediately specify the value of a primitive type variable. We thus distinguish between declaring a variable and initialising it. When we declare a variable we simply write its (primitive) type and its name without giving it any initial value. This will allocate the required amount of memory for each primitive type variable.
We have seen how long and double require 64 bits of memory, int and float require 32 bits of memory, char requires 16 bits and boolean requires 1 bit. You will see later on how most computers do not support the allocation of a singular bit of memory. This is not important to us.
To the left you can see on the top a declaration and below it an initialisation. As a side note it is important that a variable can only be declared a single time, but we will see later on that visibility of a variable might allow for multiple variables with the same name to exist.
Variables
Assignment
int x = 5;
x = 2;
x = 1;
x = x + 1;
Whenever a variable has been declared or initialised any future write to said variable is called an assignment. Even though the first write to any variable would be its initialisation depending on the situation it might not serve the function of initialising the variable. Take writing a user input to some variable multiple times as an example, then the first write is no different from the last one in terms of what they do and hence we just refer to all of them as assignments.
Any assigned value to some variables must have the same type as the variable itself or alternatively it is also possible if the value to be assigned can be implicitly casted to match the type of the variable. This is often the case with long variables being assigned.
Branching
If statements
if (condition) {
//statements
}
A very important tool in programming is branching. Branching consists of changing the flow of execution (control flow) depending on if a condition is met or not. We use if statements to have parts of a program be executed if and only if the condition of the if statement is satisfied. This is very useful as given some input we might want to change what our program does. The structure of an if statement is always the same and is shown to the left.
It is convention to use spaces between the if keyword and the parentheses as it helps to distinguish function calls and if statements. These are small things that might seem insignificant but seeing as humans are incredibly good at pattern matching you should do this as it will help you to more quickly debug your code.
Branching
If / else statements
if (condition) {
//statements
} else if (condition) {
//statements
} else {
//statements
}
One might want to specify an alternative if the if condition is not satisfied. This splits up the control flow and allows for two different ways of execution. An if statement does not need an else statement. Either the if condition is satisfied and we execute the statements is the body of the if statement (the part between the curly brackets) or we execute the statements in the else statement.
We can further extend this by using if / else if / else statements which allow us to specify an arbitrary number of alternatives each with its own condition (besides the else statement). The else statement is never mandatory but it makes sense in many cases to add an else statement.
Branching
Common fallacy
if (condition) {
//statements
}
if (condition) {
//statements
} else {
//statements
}
When given an if / else if / else statement only one of the given bodies is executed and all others are not. On the left one can see an example where the first if statement is not connected to the if / else statement following it. In the beginning it often happens that you may use multiple if statement and think only one of them will be executed in the end, this however is not the case and you must always use if / else if / else if you want to make sure that only one of the bodies is executed.
One may argue that logically only one of the conditions could hold, however it is bad practice to use multiple if statements when and if / else if statement would also work. You should program in a way where looking at the code is sufficient to understand what is happening and it should require very little logical understanding of the program to understand the basic structure of a program.
Branching
Condition
if (boolVar == true) {
//statements
}
if (boolVar == true) {
return true;
} else {
return false;
}
One thing I often see when looking at student submissions is comparing boolean variables with boolean values using the equality operator. This is redundant as the boolean variable already has one of the values true or false and hence can directly be used as the condition.
Even worse is the usage of the bottom code sample to the left. Not only does it crowd the program with unnecessary code but it also takes much longer to type. Instead of doing that you can just use "return boolVar" which does the same thing. Remember this every time you return values in a function and whenever you have a boolean variable in your if condition.
Branching
Using short circuiting to your advantage
if (x != 0 && (y / x) > 4) {
return y / x;
}
We have seen that integer division where the divisor is zero produces an error. Sometimes we would want a division to be part of a condition and hence must always verify that the divisor is not zero. One could use nested if statements here. This can be avoided by using that the logical operators && and | | short circuit and hence adding x != 0 as the first operand of the operation && with the condition doing the division with x as the divisor as the second operand will solve the problem. This is because if x were to be zero then the first operand evaluates to false and hence the second operand of the && operation is not evaluated and thus produces no error. This is a very useful trick and it pays of to remember this.