Let us now go back to expressions and operators which we have already seen in previous episodes. We can find expression in assignments, for example. The expression is necessarily on the right-hand side of the = sign and will calculate a value of the same type as the variable on the left-hand side of the = sign, since the assignment will give the value calculated by the expression to the variable. So, an expression can simply be a literal value, like 4 or 3.14 More generally, an expression is a mathematical formula implementing operators, such as here. Here, we multiply the value of the variable n by itself. By the way, the asterisk is the multiplication symbol. Here, we thus simply obtain n². More generally, an expression can be expressed using several operators and parentheses; precisely like a mathematical formula. The type of the expressions is thus important. It is also essential to be aware that literal values have their own type exactly like variables. For example, if we write the literal value 1, the type of this value is "int". However, if we write 1.0 the type of this value is double. By the way, please note that we can write the shorter " 1. " which is strictly equivalent to 1.0, thus of type double. Thus, we can simply write, for example, this declaration which is strictly equivalent to this declaration here. However " 1.0 " is a lot more readable than the short version " 1. " Therefore, prefer " 1.0 " We may also use the so-called scientific notation. For example, we can write " 2e3 " which is strictly equivalent to 2x10³ (2x10^3), that is 2000. In genere, " aeb " is equivalent to ax10^b For example, in this declaration, we initialize the variable x to the value 1.3x10³ that is, 1300. In this declaration here, we initialize the variable y to the value 1.3x10^-3 that is, 0.0013 Regarding operators, we have at our disposal, first of all, the four usual operators. For addition, we will simply use the + symbol. For subtraction the - symbol. As we have said, we use the * symbol for multiplication. And the division is written with the / character. However, there is a trick regarding the division. If both values involved in the division are of type int, it will be the integer division. For example, in this division, 1 and 2 are both of type int; this is thus the integer division. This means that we will obtain, as the division's result, the value 0. Why is that ? Because 1 = 0*2 + 1 and this 0 is the result of the integer division. Another example : 5/2 5 and 2 are both of type int. Therefore, the result will be 2, for 5 = 2*2 + 1 and this 2 here is indeed the result of the division. However, if at least one of the two values involved in the division is of type double, it will be the standard division we are familiar with. In this example, we will thus obtain the result 0.5 as we would have expected. In Java, we may also use the operators += -= *= /= For example, we may write "a += 5" where a is a variable; this is stritly equivalent to writing "a = a + 5" Another example : we may write "a *= b", where b is another variable; this is strictly equivalent to writing "a = a * b" In the case of int and int only, we may also use an operator called "modulo", written with the character % returning the remainder of the integer division. For example, if we write "11 % 4", that is, 11 modulo 4, we will obtain 3. Why so ? Because 11 = 2 * 4 + 3 This 3 is the remainder of the division 11 by 4, and thus the result of "11 % 4". Now, if we calculate "12 % 4", what will we obtain, in your opinion ? 0 Why ? Because 12 = 3 * 4 + 0 The remainder of the division 12 by 4 is thus 0 . Therefore, the value of 12 modulo 4 is indeed 0. We may also use the operators ++ and -- called the increment and decrement operators. Namely, they will add 1 to or subtract 1 from the value of a variable. For example, we may write " ++i " which is equivalent to "i = i + 1" In other words, we will add 1 to the value of the variable i. We could also have written " --a ", which is equivalent to "a = a - 1". Namely, we will subtract 1 from the value of the variable a. These two operators are frequently used with "for loops", a notion we will expand on, in an upcoming lesson. Up until now, we have always considered decimal values -the double variables- and integer values -the int variable- separately. Now, what will happen if we try to assign a decimal value to an int-type variable. Actually, in Java, it is impossible to assign a decimal value, for example of double typed value to an int-type variable. For example, we have declared here a double-type variable x. Now, we are trying to assign the value of the expression "3 * x" - which is of the type double - to our int-type variable n. The compiler will simply refuse to compile this code and will produce an error message "possible loss of precision" informing us that through this assignment, we will use the value after the period. On the other hand, it is possible to assign an int-type value to a decimal-type value, for example of the type double. Here, we have declared an int-type variable n. Now, we're trying to assign the value of the expression "2 * n" - which is of the type int" to our double-type variable x. In this case, the compiler will automatically convert from int to double, which is perfectly possible. Indeed, in this case, there is no loss of precision. Now, let us go back to the trap of the integer division. Here, we have declared a double-type variable x, represented here. Through this assignment, we assign the value of the expression 1/2 to our variable x. In your opinion, what will x contain after this assignment? 1 and 2 are both int-type literal values. This division is therefore the integer division In the case of the integer division 1 / 2 is 0. We will therefore assign the value 0 to the variable x. By the way, please note that there is an automatic conversion from int to double since this 0 here is of the type int and the 0 assigned to x is obviously of the type double. Therefore, the fact that x is of type double, has no influence of the evaluation of the expression 1 / 2 The problem will occur in practice when we try, for example, to calculate the average of two integer values. In this example, we have an int-type variable note1 (TN:"note" means "grade") initialized to 4. note2, also of the type int, is initialized to 5. Now, what will happen if we try to calculate their average through this declaration? In this expression, note1 and note2 are of type int. Their sum is therefore also of type int. So, their sum is 9, an int-type value. This 2 here, to calculate the average is an int-type literal value; this division is thus the integer division. Therefore, we will calculate the result of the integer division 9 by 2 and obtain the value 4, assigned to our variable moyenne (TN:"moyenne" means "average") This is problematic; we expected the average to be 4.5 A possible solution is to start by calculating the sum of the two int-type variables. here, we have "note1 + note2" assigned to the variable moyenne "note1 + note2" is 9, of the type int which will be assigned to the variable moyenne. Following this instruction, which is equivalent to "moyenne = moyenne / 2 ". 2 is an int-type literal value. However, moyenne is a double-type literal value. Therefore, this division is the standard division and we will, this time, obtain 4.5 in our variable moyenne. Let us conclude by saying that we may use functions and mathematical constants in Java expressions. To that end, you simply need to respect the following notations : For example, if we wish to use a function, we will start by writing "Math." followed by the function's name, followed by the values needed in the function. In the case of constants, we start, once again, by writing "Math." followed by the constant's name. For example, if you we want to calculate the sine of an angle, we will write "Math.sin()" in order to use the sine function, followed by the angle which sine we want to calculate. In Java, the trigonometric functions expect a value in radians. For example, if we have a 10 degrees angle, here, we first need to convert it into radians before feeding it to the sine function. To that end, we simply need to multiply it by "pi" and divide it by 180. We did just that here. In order to obtain the value pi, we simply wrote "Math.PI" By the way, this is not an integer division but the division we are familiar with because Math.PI is of the type double. If you are interested, you will find all the available Java functions by clicking this URL.