**A Notional Machine** --- ******************************************************************************************************** * MEMORY (long row of slots) * I/O .---------. / Code.java * .----------------------. |_________| .----------. / .--------. * | • Network (Internet) | |_________| | / | step 1 | * | • Printer | .-----. |_________| | PERSISTENT/| | step 2 <--- INSTRUCTION * | • Screen |<---->| CPU |<---->|_________|<---->| STORAGE v | | step 3 | POINTER * | • Speakers | '-----' |_________| | (HD) | | ... | * | • ... | |_________| '----------' '--------' * '----------------------' ' ' * • * • * • ******************************************************************************************************** - The CENTRAL PROCESSING UNIT (CPU) has two essential operations + do math + move data (usually by copying it from one place to another) + for example, taking two numbers stored in different memory slots, adding them together, and storing the result in a third memory slot + our notional CPU can perform these operations at a certain rate (i.e., $x$ operations per second), so if we care about the performance of a computer program, we need to think about how many operations the program will need to perform - Data is sent to and received from different places + MEMORY * a long row of slots, each of which can store a piece of information (number, text, the location of other information) * a sort of *short-term memory* because information only exists in memory while it has electricity (i.e., if the machine is turned off, memory is cleared) + PERSISTENT STORAGE * typically a computer's *hard drive*, but can take other forms (e.g., magnetic tape in ye olde days) * does not require electricity to hold information, acts as the machine's *long-term memory* * where the files and folders live - including Java files like `Code.java` containing instructions for the machine - when code is *executed*, it's copied from persistent storage to memory, and then instructions are sent to the CPU one at a time + INPUT/OUTPT (I/O) * external channels through which the machine can send and/or receive data - The INSTRUCTION POINTER (IP) is a special piece of data + it keeps track of the next step the machine should do + CPU can change it just like other data, meaning it can skip over some steps or go back and redo others - Java, unlike Python, is a compiled language + this means the code must go through a translation (i.e., must *be compiled*) before it can be run + a sketch of this process is shown below * `Code.java` is a text file containing the human-readable version of the code, just like a `.py` file * the Java compiler translates the `.java` file into a binary `.class` file (binary meaning it contains data in raw 1s and 0s that can't directly be interpreted as text) - this format is also referred to as *Java bytecode* * to run the compiled program, the `.class` file is given as input to the Java Virtual Machine, which interprets the Java bytecode and sends the corresponding instructions to the CPU ********************************************************************************************************************** * * Code.java Code.class * .--------. .--------. * | step 1 | .---------------. | 011100 | .----------------------------. * | step 2 | ---------> | Java compiler | ----------> | 001101 | ------------> | Java Virtual Machine (JVM) | * | step 3 | '---------------' | 011001 | '----------------------------' * | ... | | ... | * '--------' (an application '--------' (an application usually written in * (human-readable written in Java) (machine-readable C that interprets .class files and * text file) binary file) sends instructions to CPU) * * **********************************************************************************************************************