Until now, we have learnt how to declare, initialize and assign a variable. Today, we will learn how to print a variable and read its value from the keyboard. Let us begin with the printing. Paying attention to this example, the printing lines are these three lines here. Let us examine them thoroughly. The line interesting us is the following. If we suppose that we have an int-type variable n, containing the value 4, this line will print the message : "La variable n contient 4." (TN: meaning "the variable n contains 4.") This line is composed of two parts. First of all, the description, in the Java language of the technically called "standard output" or terminal. The second part is the message to be sent to the terminal. This message is split in several pieces. Between quotation marks, we have a literal string which will be written as such on the output. Then comes a Java expression, that is, something which will be evaluated before the printing. The evaluation picks up the value of n, thus printing 4. Then comes the third and last part : another literal string, reduced to a period which will be written as such. These different parts are composed with the sign '+' . Be careful though, the '+' sign, simply assembles the messages one after the other (= concatenates). If we write "un", between quotation marks followed by "message", this corresponds to the string "un message" (TN: means "a message"). '+' stands thus here for message concatenation. It is a stacking of the messages, one after the other and not at all an addition. On the other hand, we have the terminal description : "system", period, "out", period and then a function, a method, println() which means "print line". This instruction will print the message and trigger a line break just after. If we do not want this line break, we can use the method "print() ", simply (without "ln"), which which will not trigger a line break. To sum up, if you wish to print an expression, a message, type "system.out.println()" if you want a line break; then, between quotation marks, the literal messages which will be printed exactly as you write them and expressions, concatenated, combined with the '+' sign. If you have no desire for a line break, use the function "print()" instead of the function "println()"; the different messages will then be printed one after the other, glued together and not line by line. We can use this system to simply print the value of a variable, reduced to the expression of this value. But we can also combine variables using any Java expression. For example, we can print a message starting with "Le double de", then evaluate the value of n (TN: "Le double de" means "The double of" ), add the message "est" and evaluate (TN: "est" means "is") an expression as complex as you desire, which value will be printed. Previously, we insisted on the fact that the sign used to glue, concatenate strings was not an arithmetic addition. So, how do me make an addition? For that, we absolutely need to resort to parentheses, so that the expressions in parentheses will be evaluated in an arithmetic way and not in the way of strings glued one after the other. For example, if n is 4 and nCarre is 16, this expression will evaluate n, which contains 4, +, in an arithmetic sense, 16; thus returning the value 20. This value will be printed as such. The final printing will thus be "n plus nCarre = 20" However, if we omit the parentheses, we will print "n plus nCarre =", followed by the value 4, immediately followed by the value 16. We thus get the message : "n plus nCarre = 416". This is clearly not what we wanted. In order to make sure that the evaluation of "n + nCarre" will be done in an arithmetic way, we need to protect it with parentheses. Now, let us unfold the course example step by step. We begin by declaring a variable n, intialized to the value 4. Then we declare a variable nCarre which we do not initialize; we do not know its initial value. Now, we execute an assignment, evaluating the expression "n * n", that is, 4 times 4, returning 16. This value 16 will be copied and stored inside nCarre. In the memory, we thus have the value 4 inside n and 16 inside nCarre. Now, we will print the message "La variable n contient", then evaluate the expression "n", that is, replace it by the value of n. We will thus get the message"La variable n contient 4" (TN: means "The variable n contains 4") Now, we move on to the next line, printing the message : "Le carré de n", then evaluate the expression "n", (TN: "le carré de n" means "the square of n") that is, replace it by its value 4 and add the literal string "est". Then, we evaluate the expression "nCarre", thus replacing it by its value 16 and concatenate, push back the literal string containing the period. Therefore, the final message will be "Le carré de 4 est 16." Finally, in the last line, we will similarly print the message "Le double de n est". (TN: "Le double de n est" means "the double of n is" ) By the way, please note that it is the letter n that will be printed since it is included in the quotation marks. This is the word "n" and not an expression such as here. "Le double de n est", followed by the expression "2 * n". We will thus search for the value of n, that is, 4 and multiply it by 2, returning 8. We thus get the message "8", followed by the message "." and, finally, a line break due to "println()" A little question now. Let us detail step by step the execution of the question code. We begin by declaring an int-type variable a, initialized to the value 2. Then, we declare an int-type variable b, initialized to the value 1. Now, we move on to an assignment, evaluating a, that is, 2 then evaluating "b + 2", that is "1 + 2" returning the value 3. We thus get the value "2 * 3" which will be stored inside b. To sum up, this expression will be evaluated to 6 and the value 6 will be stored inside b. Now, we move on to the last line which will print the expression a, that is, 2, then the string "," then the value of b, that is, 6. Another question now. Let us examine the step by step unfolding of the question code. We begin by declaring an int-type variable a, intialized to the value 1. Then, we declare an int-type variable b, intialized to the value 2. Now, we move on to an assignment, evaluating the expression b, thus returning 2. We will store this 2 inside a and evaluate the next expression, a and copy this expression inside b. Therefore, during the printing, we will evaluate the expression a, that is 2, then the literal string "," then the evaluation of the expression b, that is, 2. The printing will thus be "2, 2". At this point, we could wonder how to swap the value of two variables. Indeed, in the previous example, we have not swapped the two variables, but simply copied the value of b inside both variables a and b. Comparatively, we could wonder how to swap the content of two glasses. Let us suppose that you have a glass of grenadine and a glass of mint. The mint glass will be called b and the grenadine glass, a. How to proceed in order to swap the content of both glasses ? The correct solution is to introduce a third glass called "tmp". It is not any more difficult in programming. In order to swap the content of two variables, we simply need to introduce a temporary variable, making it possible to transfer the values. For example, a is initialized to 1 and b is initialized to 2. We introduce a temporary variable "temp" in which we will copy the value of a. Then we will copy the value of b inside a. Since the value of a is saved inside "temp", we can now replace the initial value of a by b's. And, finally, we will copy the value saved in "temp" inside the variable b, namely we store the value 1 inside the variable b. Initially, a contained 1 and b contained 2; we have thus swapped the initial content of the two variables a and b. Now, let us move on to the reading of the value of a variable from the keyboard. Let us reuse our example of the calculation and printing of a number's square or double. Let us suppose that, this time, we do not want the value of n to be 4, but instead ask the user to enter the value of n through the keyboard. How should we proceed ? Well, we will simply replace the line assigning the value 4 to n by a few lines which we are going to detail soon. These lines will permit to assign a value, read from the keyboard, to the variable n. First of all, we need to import the so-called "Scanner class" by adding this line once at the beginning of the program : import java.util.Scanner; This line added, you can declare a Scanner-type variable "keyb" and initialize it by binding it to the keyboard. This is technically called the standad input device. This input is represented by "System.in". All you need to do is to write this line : Scanner, followed by a variable name (e.g, keyb) = new Scanner(System.in); Thanks to that, you can use the variable "keyb" in order to read variables from the keyboard. This line will be written once and only once in the whole program. You only need to create the link towards the keyboard once. At this point of the course, you will write it in the "main". From this line, you can use the variable keyb in order to read variables. We may want to read an integer from the keyboard in order to initialize a variable like we do here: we declare an int-type variable n and initialize it with a keyboard reading. This is written as follows: keyb (the Scanner-type variable we have previously declared), period, and the reading of an integer nextInt() in order to read the next integer, parentheses and a semicolon to close the initialization instruction of our variable. This might all seem technical but you merely need to follow this three steps procedure which we will go through once more. First of all, you add this line once at the very beginning of your program. Then, you write this line once where you need it; here, in the main in order to declare a Scanner-type variable which you will use every time you need to read an integer thanks to "nextInt()". The "nextInt()" method works as follows. It begins by momentarily stopping the progess of the program. Then, it will wait until the user types a value and hits the "enter" or "return" key. Then, it will read the value as an integer, convert it into an integer. This integer-type value, converted from what has been entered through the keyboard will be assigned to the variable n during the third step. The program will then resume. "nextInt()" is what we call a method, a function, a processing, associated to the Scanner object. It is possible to read many other things, many different types of values. For example, if we want to read a decimal-type value, a double-type value, we will use the function "nextDouble()". In order to initialize a double-type variable x to a value read from the keyboard, we will write the declaration double x and the initialization = keyb.nextDouble(); which will read a double-type value from the keyboard. Let us take a step by step example. We start with the usual declaration line, creating a Scanner which will associate the keyboard to the Scanner "keyb". Then, we will warn the user that we expect him to enter a value. We will print for him, with the way we have just seen, a message : "Entrez une valeur pour n" (TN: "Enter a value for n") We will read a value from the keyboard thanks to the method nextInt() of the previously declared object "keyb". Let's assume that the user types the value 2 on the keyboard. This method will be evaluated and will read the value 2 from the keyboard. 2 will then be stored as the initial value of the variable n. At the next step, we will evaluate "n * n", that is, 4. We will store this 4 inside the variable nCarre and print the message "La variable contient" and evaluate the expression n. Thus, we will print the value of n. In addition to nextInt(), reading an integer and nextDouble(), reading a decimal, a useful function is the function nextLine() permitting to read a whole line in one go. Let us take an example. As usual, we declare a Scanner-type variable keyb, associated to the keyboard. Once this declaration has been done, we can call our function keyb.nextLine(); thus reading a whole line. If the user has typed a message such as "bonjour tout le monde !" (TN:"hello everyone!") the call to the function nextLine() will read the whole line entered by the user, every character typed, until the user has pressed the enter or return key. The question is, in what variable will we store this message? What is its type? For now, anticipiting on forthcoming material, we ask you to copy this line where "String" is the type of the variable s, used in order to save the message. s is the name of the variable. Once we have typed the line String s = keyb.nextLine(); s will contain all the characters entered by the user until they have pressed the return key. Be careful though when using the nextLine() method: it is not very compatible with nextInt() or nextDouble(). For example, if we declare an int-type variable i and initialize it through keyboard reading with nextInt() and that, right away, we wish to read a message, an entire line with nextLine() which we store in s1 and that we read another line similarly, and store it, initializing a variable s2. The idea is to read an integer and then to read the end of the line and then another line. If we type the message "25 francs", line break, followed by "23 francs" what will happen is that we will start by reading an integer. i will thus contain 25. Only then will we read the end of the line until here, "francs". Thus, s1 will indeed contain "francs". Finally, we will read a whole line, that is, "23 francs". Until now, no problem. Now, if we type 14, line break, "euros", line break,43 what will happen ? Let us analyze step by step. We want to read an integer : i will thus contain 14. Then, we will read the end of the line; it is empty here. s1 will be empty and we will read the next line : "euros" will be stored inside s2. So, s2 will contain "euros". What we see here is that the combination of nextInt() followed by nextLine() does not always work properly. Especially since, here, the user has generated a line break right after the integer 14 and the reading of nextLine() after nextInt(), typed on the keyboard will read the end of the line, corresponding to the line break, the return key typed by the user. This occurs because nextInt() ends its reading just after the integer, here, 14 and does not read the return character, the return key. How should we proceed, then? Every time you wish to read a number with nextInt() or nextDouble() and that the next reading is a line, you need to anticipate the non-reading of the line break by nextInt() or nextDouble() by adding a reading of the end of the line, a supplementary nextLine() inserted between the integer/decimal reading and the reading of the next message, which you really want to read. You add a reading in a variable, previously declared to that effect String s = keyb.nextline(); Later, once you will be familiar with control structures, especially with conditional branches or conditional loops, you will be able to make sure that the reading of s is indeed an empty character string, or, if not, you will take it as the message to read. If s is empty, you will take the next line as the desired message.